Spring在运行时将一些bean添加到服务上下文中

时间:2016-01-13 20:21:43

标签: java spring servlets initialization javabeans

我正在创建spring dispatcherServlet并以这种方式设置他的servlet上下文:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;


import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.*;

public class MyDispatcherBean implements InitializingBean {


    @Autowired
    private ApplicationContext applicationContext;

    private DispatcherServlet dispatcherServlet;

    private final String someContext = "some.xml";

    private ServletContext servletContext;


    public void execute(/* Do some code..*/) throws IOException {

        /*
        Do some code..
        */
        try {
            dispatcherServlet.service(/* ...*/);
        } catch (ServletException e) {

        }

    }

    public WebApplicationContext getServiceContext() {
        return dispatcherServlet.getWebApplicationContext();
    }
    public void afterPropertiesSet() throws Exception {

        dispatcherServlet = new DispatcherServlet() {

            private static final long serialVersionUID = -7492692694742330997L;

            @Override
            protected WebApplicationContext initWebApplicationContext() {
                WebApplicationContext wac = createWebApplicationContext(applicationContext);
                if (wac == null) {
                    wac = super.initWebApplicationContext();
                }
                return wac;
            }

        };

        dispatcherServlet.setContextConfigLocation(someContext);
        dispatcherServlet.init(new DelegatingServletConfig());
    }

    private class DelegatingServletConfig implements ServletConfig {

        public String getServletName() {
            return "myDispatcher";
        }

        public ServletContext getServletContext() {
            return MyDispatcherBean.this.servletContext;
        }

        public String getInitParameter(String paramName) {
            return null;
        }

        public Enumeration<String> getInitParameterNames() {
            return Collections.enumeration(new HashSet<String>());
        }
    }

}

我用这种方式创建了这个bean:

<bean id="myDispatcher" class="some.package.MyDispatcherBean " />

然后在我的代码中的任何地方都可以存在任意数量的bean,其代码可以获得&#34; myDispatcher&#34; bean并在运行时将一些bean添加到DispatcherServlet的servlet上下文:

public class ContextAdder implements InitializingBean {


    @Autowired
    private ApplicationContext applicationContext;


    public String classPathToContext;

    public void afterPropertiesSet() throws Exception {


        DispatcherServlet dispatcherServlet = applicationContext.getBean("myDispatcher",DispatcherServlet.class);
        XmlWebApplicationContext webApplicationContext = (XmlWebApplicationContext) dispatcherServlet.getWebApplicationContext();
        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader((DefaultListableBeanFactory) webApplicationContext.getBeanFactory());
        xmlReader.loadBeanDefinitions(new ClassPathResource(classPathToContext));

    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public String getClassPathToContext() {
        return classPathToContext;
    }

    public void setClassPathToContext(String classPathToContext) {
        this.classPathToContext = classPathToContext;
    }
}

这些bean是以这种方式创建的:

<bean id="adder1" class="stargate.sg_1.ContextAdder" depends-on="myDispatcher">
        <property name="classPathToContext" value="Optimus.xml" />
</bean>

<bean id="adder2" class="stargate.atlantida.ContextAdder" depends-on="myDispatcher">
        <property name="classPathToContext" value="StarShip.xml" />
</bean>

最后我希望行为:每个ContextAdder bean都将新bean添加到myDispatcher bean中dispatcherServlet的servlet上下文。

但我害怕片刻:

  1. 这个问题只有一个决定吗?
  2. 这是最好的吗?
  3. 每当我将新bean添加到DispatcherServlet的servletContext时 - 它会调用ServletContext甚至ApplicationContext的刷新吗?如果是真的 - 它对性能有影响吗?
  4. 所有生命周期都是正确的吗?

1 个答案:

答案 0 :(得分:1)

我相信有一种更好的方法,但会留给你决定。

使用您的方法(实现InitializingBean),您将在bean定义阶段完成后调用bean创建代码,并在构建bean之后 。您的bean定义阶段非常简单(只需创建&#34; myDispatcher&#34;)。

我建议您在bean定义阶段创建/加载所有bean定义。实现此目的的一种方法是改为挂钩BeanFactory后处理器(实现BeanFactoryPostProcessor)。在此阶段,Spring允许您修改现有的bean定义,更重要的是,您可以添加更多的bean定义。现在,当您离开此阶段时,将在一个阶段中创建所有bean。方法很自然:创建bean定义=&gt;创建bean并连线=&gt;完成。

public class ContextAdder implements BeanFactoryPostProcessor {

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
        throws BeansException {

    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader((BeanDefinitionRegistry)factory);

    // I)  LOAD BY PATTERN MATCHING
    //PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(factory.getBeanClassLoader());
    //for (Resource resource : resourceResolver.getResources("com/.../*.xml"))
    //reader.loadBeanDefinitions(resource);

    // II)  LOAD A SINGLE FILE AT A TIME
    reader.loadBeanDefinitions(new ClassPathResource("com/../Optimus.xml""));
    .....
}

也许你可以根据自己的独特需求采用这个概念。