In fact, the configuration file is not loaded with the server startup is also possible, but this operation, each time you get the corresponding object, will go to read the configuration file, thereby reducing the efficiency of the program, and Spring has provided us with a listener to listen to the server is started, and then at startup, Load the spring configuration file and load it only once, improving program efficiency.
Implementation: Its configuration needs to be carried out in Web. XML, specifically implemented as follows:
<!--Configuring Listeners - <!--to load the spring configuration file when the server starts - <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener> <!--Configuring the Spring configuration file - <Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:spring/spring-context.xml</Param-value> </Context-param>
Note: Note here that the contents of <param-name> in the <context-param> tag are fixed-value,<param-value> in fixed format: classpath: Spring configuration file (if you have a path, add it)
The above configuration allows the spring configuration file to be loaded with the server startup.
by Shawn Chen 2018.1.10 Night
Spring Web Project Spring configuration file loads automatically with server startup