Spring: Make ServeltDispatcher Context await until another Context finish loading

Rudziankoŭ :

I would like to load DBbuildServletDispatcher, after finishing Context loading, eg on ApplicationListener<ContextRefreshedEvent> event fire message to build(or proceed building) AppServletDispatcher Context

SpringMultiDisp

In other words could AppDispatcher Context wait until finishing creation of DBbuild Context? Are there any common ways of doing this?

hi.nitish :

It is simple. Remember the param scope is of two types - context param and servlet's init param. What you need is all dependencies must be initialised before the child context is loaded. Here DBbuildServletDispatcher should be initialised in the parent context, that is ApplicationContext, and AppServletDispatcher is 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 context param loads the context file and create the ApplicationContext. The second part defines the WebApplicationContext. Refer here and WebApplicationContextUtils may also be utilized.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=460917&siteId=1