【发布时间】:2016-04-18 23:53:09
【问题描述】:
我正在创建 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 " />
然后在我的代码中的任何地方都可以存在任意数量的带有获取“myDispatcher”bean的代码的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 上下文中。
但我害怕某些时刻:
- 这个问题只有一个决定吗?
- 这是最好的吗?
- 每次我向 DispatcherServlet 的 servletContext 添加新 bean 时,它会调用 ServletContext 甚至 ApplicationContext 的刷新吗?如果是真的 - 它对性能有影响吗?
- 所有生命周期都正确吗?
【问题讨论】:
标签: java spring servlets initialization javabeans