Spring: get the ServeltDispatcher context to wait for the Context to load

I would like to load the DBbuildServletDispatcher after completing the loading context , for example, in the ApplicationListener<ContextRefreshedEvent> message about fire events for the assembly (or continue building) AppServletDispatcher Context

SpringMultiDisp

In other words, can the AppDispatcher Context wait until the DBbuild Context ? Are there any general ways to do this?

+7
java spring servlets
source share
3 answers

It's simple. Remember that the parameter area has two types: param parameter and init parameter. All you need is all the dependencies that must be initialized before loading the child context. Here DBbuildServletDispatcher should be initialized in the parent context, i.e. ApplicationContext , and AppServletDispatcher be WebApplicationContext , which is the child context of the application context

 <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/DBbuildServletDispatcher.xml </param-value> </context-param> <servlet> <servlet-name>firstServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/AppServletDispatcher.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>my-servlet</servlet-name> <url-pattern>/abc/* </url-pattern> </servlet-mapping> 

The first part with the context parameter loads the context file and creates the ApplicationContext. The second part defines the WebApplicationContext. WebApplicationContextUtils can also be used here .

+6
source share

Something like this might work. You are loading only DBbuildContext.xml (applicationContext.xml)

after adding this line:

 <bean id="eventListenerBean" class="abcApplicationListenerBean" /> 

Then define a class

 package abc; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; public class ApplicationListenerBean implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextRefreshedEvent) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("appServletContext.xml"); } } } 

Now appServletContext.xml will only create context when loading the dbBuildContext.xml file.

+2
source share

Like any regular WebServlet, an instance of the dispatcher servlet can be ordered in web.xml using <load-on-startup>

Looking for servlet 2.5 web.xml xsd

The load-on-startup element indicates that this servlet should be loaded (an instance is created and its init () is called) when the web application starts. The optional content of this element must be an integer indicating the servlet load order. If the value is a negative integer, or the element is missing, the container can load the servlet whenever it selects. If the value is a positive integer or 0, the container must load and initialize the servlet when the application is deployed. The container must ensure that servlets with the smallest integers are loaded before the servlets marked with higher integers. The container can choose the servlet loading order with the same load value at startup.

So, to boot DBbuildServletDispatcher first, make sure its load-on-startup less than load-on-startup AppServletDispatcher

 <servlet> <servlet-name>DBbuildServletDispatcher</servlet-name> <servlet-class>com.foo.DBbuildServletDispatcher</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet> <servlet-name>AppServletDispatcher</servlet-name> <servlet-class>com.foo.AppServletDispatcher</servlet-class> <load-on-startup>1</load-on-startup> </servlet> 
+1
source share

All Articles