Spring Web project spring configuration files are loaded automatically when the server starts

  • 2020-12-19 21:03:13
  • OfStack

Preface: in fact is not with the server startup configuration file loading is also possible, but such operations, each time to obtain the corresponding object, will go to read one configuration file, and reduces the efficiency of the program, and has provided us in Spring listener, can monitor whether the server startup, and then at startup, loading spring configuration file, and load only once, so as to improve the efficiency of the program.

Implementation: Its configuration needs to be carried out in ES5en.xml. The specific implementation is as follows:


<!-- Configure listeners  --> <!-- To load when the server starts spring The configuration file -->
  <listener>
    <listener-class>
     org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <!-- configuration spring The configuration file -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/spring-context.xml</param-value>
  </context-param>

Note: Note here < context-param > In the label < param-name > Is a fixed value, < param-value > classpath:spring configuration file (add if there is a path)

The above configuration allows the spring configuration file to be loaded as the server starts.

conclusion


Related articles: