Spring Web project spring configuration files are automatically loaded when the server is started
In fact, the configuration file does not load when the server is started. However, in this way, the configuration file will be read every time the corresponding object is obtained, thus reducing the program efficiency, spring provides a listener for us to monitor whether the server is started, and then load the spring configuration file only once at startup to improve program efficiency.
Implementation: The configuration must be performed in web. xml. The specific implementation is as follows:
<! -- Configure the listener --> <! -- Load the spring configuration file --> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> </listener> <! -- Configure the spring configuration file --> <context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: spring and spring-context.xml </param-value> </context-param>
Note: The content in the <context-param> label <param-name> is a fixed value, and the content in <param-value> is in a fixed format: classpath: spring configuration file (add a path if any)
With the above configuration, you can load the spring configuration file as the server starts.