Spring Web上下文被加载两次

时间:2018-04-12 13:02:37

标签: java spring

我可以在我的日志中看到我的Web应用程序被初始化两次,如下所示。

04-12@12:10:38 INFO ContextLoader:305 - Root WebApplicationContext: initialization started

我有以下web.xml配置。请告知此配置有什么不妥。

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/spring/root-context.xml,
        /WEB-INF/spring/spring-security.xml 
        </param-value>
    </context-param>
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  

更新 - 我尝试了java配置以及后续问题仍然存在。这让我想知道我做错了什么!

   XmlWebApplicationContext context = new XmlWebApplicationContext();
    context.setConfigLocations("/WEB-INF/spring/root-context.xml","/WEB-INF/spring/spring-security.xml" ,"/WEB-INF/spring/appServlet/servlet-context.xml");

    container.addListener(new ContextLoaderListener(context));

    XmlWebApplicationContext webContext = new XmlWebApplicationContext();
    webContext.setConfigLocations("/WEB-INF/spring/appServlet/servlet-context.xml" );

    ServletRegistration.Dynamic dispatcher = container
      .addServlet("dispatcher", new DispatcherServlet(webContext));

    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");

1 个答案:

答案 0 :(得分:1)

尝试使用以下方法更改context-param:

  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath*:META-INF/spring/*.xml</param-value>
 </context-param>