Reprinted blog: http://blog.163.com/zhangke_616/blog/static/191980492007994948206/
In the actual project spring's configuration file Applicationcontext.xml is loaded through spring provided by the loading mechanism, automatically loaded in the container, in the Web project, the configuration file is loaded into the Web container for parsing, currently, Spring provides two loaders for the Web container to load: One is Contextloaderlistener, the other is Contextloaderservlet. The two are functionally identical, but one is based on the newly introduced listener interface implementation in the Servlet2.3 version, and the other is based on the servlet interface, and the following are the timing applications of these two loaders in Web. xml:
The first type:
< Listener > < Listener-class >org.springframework.context.ContextLoaderListener</listener-class> </listener>
Another type of:
<servlet> <Servlet-name>Context</Servlet-name> <Servlet-class>Org.springframework.context.ContextLoaderServlet</Servlet-class> <Load-on-startup>1</Load-on-startup></servlet>
With the above configuration, the Web container will automatically load Applicationcontext.xml initialization.
If you need to specify a location for the configuration file, you can specify it by Context-param:
< Context-param > < Param-name >contextconfiglocation</param-name><Param-value >/web-inf/myapplicationcontext.xml</param-value> </ Context-param >
After that, you can
The Webapplicationcontextutils.getwebapplicationcontext method gets a reference to the ApplicationContext in the Web App.
Configuration in Spring Web. XML