I am creating spring dispatcherServlet and set his servlet context this way:
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>());
}
}
}
And i create that bean this way:
<bean id="myDispatcher" class="some.package.MyDispatcherBean " />
Then in any place in my code can exist any numbers of beans with code that get "myDispatcher" bean and add some beans in Runtime to servlet context of DispatcherServlet:
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;
}
}
These beans are created this way:
<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>
In finally I expect behaviour: each ContextAdder bean add new beans to servlet context of dispatcherServlet in myDispatcher bean.
But i am afraid of some moments:
- Is it only one decision of this problem?
- Is it the best one?
- Each time when i add new bean to servletContext of DispatcherServlet - will it invoke refresh of ServletContext or even ApplicationContext? If it is true - does it have influence on performance?
- Is all lifecycles is correct?