Spring configuration file is loaded two times

Source: Internet
Author: User
Tags xml example

The following Web. XML Example:

1. Load Contextconfiglocation with spring configuration

2. Configuring the Spring-mvc Contextconfiglocation

<servlet>    <Servlet-name>Spring-mvc</Servlet-name>    <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>    <Init-param>      <Param-name>Contextconfiglocation</Param-name>      <Param-value>Classpath:spring/spring-*.xml</Param-value>    </Init-param>    <Load-on-startup>1</Load-on-startup>  </servlet>  <servlet-mapping>    <Servlet-name>Spring-mvc</Servlet-name>    <Url-pattern>/</Url-pattern>  </servlet-mapping> <Listener>    <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>  </Listener>  <Context-param>    <Param-name>Contextconfiglocation</Param-name>    <Param-value>Classpath:spring/spring-*.xml</Param-value>  </Context-param>

Looked at the configuration, spring the following file was loaded once in SPRINGMVC and loaded again in the context.

You can see it from the Start log and start time. add: Repeated loading also causes the configured Task,schadule task to execute repeatedly!

Therefore, it is reasonable to take the definition of SPRING-MVC as a separate definition when the Web container starts to load, the name is changed to Servlet-mvc.xml to avoid being loaded repeatedly by spring, and in the spring core XML definition, do not appear MVC

<servlet>      <Servlet-name>Spring-mvc</Servlet-name>      <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>      <!--find the XML file configuration for the controller location -      <Init-param>          <Param-name>Contextconfiglocation</Param-name>          <!--find here can be configured multiple, separated by commas or with a wildcard * that is, the second line -          <Param-value>/web-inf/classes/servlet-mvc.xml</Param-value>      </Init-param>      <Load-on-startup>1</Load-on-startup>      <async-supported>True</async-supported>  </servlet>  

SPRINGMVC XML is configured separately, spring itself tube spring, springmvc tube springmvc, so OK

Summary: The separate configuration should not be combined. Note that the code uses

Spring configuration file is loaded two times

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.