Spring:使ServeltDispatcher Context等待直到另一个Context完成加载

Rud*_*koŭ 7 java spring servlets

我想DBbuildServletDispatcher在完成Context加载后加载,例如在 ApplicationListener<ContextRefreshedEvent>事件触发消息上构建(或继续构建)AppServletDispatcher Context

SpringMultiDisp

换句话说可以AppDispatcher Context等到完成创作DBbuild Context?这有什么常见的方法吗?

hi.*_*ish 6

很简单.请记住,param范围有两种类型 - 上下文参数和servlet的init参数.您需要的是必须在加载子上下文之前初始化所有依赖项.这里DBbuildServletDispatcher应该在父上下文中初始化,即ApplicationContext,并且AppServletDispatcherWebApplicationContext,它是应用程序上下文的子上下文

<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>
Run Code Online (Sandbox Code Playgroud)

带有上下文参数的第一部分加载上下文文件并创建ApplicationContext.第二部分定义WebApplicationContext.请参阅此处,也可以使用WebApplicationContextUtils.