java 如何在 Spring XML 元数据配置中为 bean 设置 ServletContext 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26632529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set ServletContext property for a bean in Spring XML metadata configuration
提问by phoenix
I tried searching here on SO but i couldn't find a solution. I have some XML metadata like the following.
我尝试在 SO 上搜索,但找不到解决方案。我有一些 XML 元数据,如下所示。
<bean class="javax.servlet.ServletContext" id="servletContext" />
<bean class="com.abc.ProductController">
<property name="servletContext" ref="servletContext"/>
</bean>
With this configuration I am getting an exception saying that "javax.servlet.ServletContext"
is an interface and it couldn't create a bean with the id servletContext
. The ProductController class is in some jar which I can't modify but I want it as a bean in my application. It has ServletContext property autowired.
使用此配置,我收到一个异常,说这"javax.servlet.ServletContext"
是一个接口,它无法创建带有 id 的 bean servletContext
。ProductController 类位于一些我无法修改的 jar 中,但我希望它作为我应用程序中的一个 bean。它具有自动装配的 ServletContext 属性。
回答by Serge Ballesta
If you need to create a bean for ServletContext
in a XML config spring application, you could use a BeanFactory<ServletContext>
implementing ServletContextAware
如果您需要ServletContext
在 XML 配置 spring 应用程序中创建一个 bean ,您可以使用一个BeanFactory<ServletContext>
实现ServletContextAware
public class ServletContextFactory implements FactoryBean<ServletContext>,
ServletContextAware{
private ServletContext servletContext;
@Override
public ServletContext getObject() throws Exception {
return servletContext;
}
@Override
public Class<?> getObjectType() {
return ServletContext.class;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
}
You can then declare :
然后您可以声明:
<bean class="org.app.ServletContextFactory" id="servletContext" />
<bean class="com.abc.ProductController">
<property name="servletContext" ref="servletContext"/>
</bean>
回答by Stefan
Just autowire the context in your controller:
只需在控制器中自动装配上下文:
@Autowired
private ServletContext context;
回答by Steve C
You cannot reference the servlet context in your XML like this because its lifecycle is controlled by the servlet container.
您不能像这样在 XML 中引用 servlet 上下文,因为它的生命周期由 servlet 容器控制。
The solution is to have com.abc.ProductController
implement ServletContextAwareand then Spring will set it for you.
解决方案是com.abc.ProductController
实现ServletContextAware,然后 Spring 会为您设置它。
回答by Ady Junior
With java config use ServletContextFactorycreated by Serge Ballesta above and:
使用 java 配置,使用上面由 Serge Ballesta 创建的ServletContextFactory和:
@Configuration
public class WebAppConfiguration {
@Autowired
private ServletContextFactory servletContextFactory;
@Bean
public ServletContextFactory servletContextFactory() {
return new ServletContextFactory();
}
}