简体   繁体   English

如何在Spring XML元数据配置中为bean设置ServletContext属性

[英]How to set ServletContext property for a bean in Spring XML metadata configuration

I tried searching here on SO but i couldn't find a solution. 我尝试在这里搜索,但我找不到解决方案。 I have some XML metadata like the following. 我有一些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 . 使用此配置,我得到一个例外,说"javax.servlet.ServletContext"是一个接口,它无法使用id servletContext创建一个bean。 The ProductController class is in some jar which I can't modify but I want it as a bean in my application. ProductController类在某个jar中,我无法修改,但我希望它在我的应用程序中作为bean。 It has ServletContext property autowired. 它具有自动装配的ServletContext属性。

If you need to create a bean for ServletContext in a XML config spring application, you could use a BeanFactory<ServletContext> implementing ServletContextAware 如果需要在XML config spring应用程序中为ServletContext创建bean,可以使用实现ServletContextAwareBeanFactory<ServletContext>

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>

Just autowire the context in your controller: 只需在控制器中自动连接上下文:

@Autowired
private ServletContext context;

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 ServletContextAware and then Spring will set it for you. 解决方案是让com.abc.ProductController实现ServletContextAware然后Spring会为你设置它。

With java config use ServletContextFactory created by Serge Ballesta above and: 使用java配置使用上面由Serge Ballesta创建的ServletContextFactory和:

@Configuration
public class WebAppConfiguration {

    @Autowired
    private ServletContextFactory servletContextFactory;

    @Bean
    public ServletContextFactory servletContextFactory() {
         return new ServletContextFactory();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Spring XML配置来设置包含特定类型的所有bean的列表的bean属性? - How can I use Spring XML configuration to set a bean property with list of all beans of a certain type? 使用来自其他bean属性的xml配置设置Spring bean属性 - Spring bean property set using xml configuration from other bean property 如何使用Spring配置将文本文件内容设置为bean属性 - How to set text file content to bean property using Spring configuration Spring 2.5 dispatcher.xml配置,“无法设置bean属性&#39;methodNameResolver&#39;”错误 - Spring 2.5 dispatcher.xml configuration, “can not set bean property 'methodNameResolver'” error 在Spring 3.2 MVC配置中设置ServletContext的属性 - Set attributes of ServletContext in Spring 3.2 MVC configuration 在 Spring Boot 中使用 @Bean 配置设置类属性的默认值 - Set default value of class property with @Bean configuration in Spring Boot 如何将Class值设置为spring bean属性? - How to set Class value to spring bean property? Spring 3在自定义bean中接收servletContext - Spring 3 receive servletContext in custom bean 如何将一个 spring bean 的父属性设置为另一个 bean 的属性? - How to set the parent attribute of one spring bean to a property of another bean? 如何在Spring中定义String数组的bean(使用XML配置) - How to define a bean of String array in Spring (using XML configuration)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM