The applicationcontext class is a spring container, which is responsible for managing all components, from the business logic layer components to the persistent layer components, must run in the spring container. Therefore, the spring applicationcontext instance must be created when the web application is started. To enable spring containers to automatically load data, the following methods are generally used:
1. Use contextloaderlistener to create applicationcontext.
Spring provides an implementation class contextloaderlistener of servletcontextlistener, which can be used as listener and will automatically find the applicationcontext. xml file under/WEB-INF/at creation. Therefore, if there is only one configuration file named applicationcontext. XML, you only need to add the following code to the Web. xml file:
<Listener>
<Listener-class>
Org. springframework. Web. Context. contextloaderlistener
</Listener-class>
</Listener>
If multiple configuration files need to be loaded, use the <context-param> element to determine the configuration file name. Because the contextloaderlistener will find the parameter named contextconfiglocation when loading. Therefore, the parameter name for <context-param> configuration should be contextconfiglocation. If multiple configuration files are loaded, add the following code to the Web. xml file:
<Context-param>
<! -Parameters required for configuring multiple files -->
<Param-Name> contextconfiglocation </param-Name>
<! -You can load multiple configuration files separated by commas (,), spaces, or semicolons. -->
<Param-value>/WEB-INF/applicationcontext. XML,/WEB-INF/service. xml </param-value>
</Context-param>
<Listener>
<Listener-class>
Org. springframework. Web. Context. contextloaderlistener
</Listener-class>
</Listener>
2. Use load-on-startup servlet to create applicationcontext.
Spring provides a special servlet class: contextloaderservlet. The servlet will automatically find the applicationcontext. xml file under/WEB-INF/at startup. To enable the contextloaderservlet to start with the application, configure the Servlet as the load-on-startup servlet. The load-on-startup value is a little smaller, because the applicationcontext must be created first. If there is only one configuration file named applicationcontext. XML, add the following code to the Web. xml file:
<Servlet>
<Servlet-Name> context </servlet-Name>
<Servlet-class>
Org. springframework. Web. Context. contextloaderservlet
</Servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
This servlet is used to provide the "backend" service. It is used as a container to manage other beans in the application and does not need to respond to customer requests. Therefore, you do not need to configure servlet-mapping.
If multiple configuration files need to be loaded, use the <context-param> element to determine the configuration file name. Because the contextloaderlistener will find the parameter named contextconfiglocation when loading. Therefore, the parameter name for <context-param> configuration should be contextconfiglocation. If multiple configuration files are loaded, add the following code to the Web. xml file:
<Context-param>
<! -Parameters required for configuring multiple files -->
<Param-Name> contextconfiglocation </param-Name>
<! -You can load multiple configuration files separated by commas (,), spaces, or semicolons. -->
<Param-value>/WEB-INF/applicationcontext. XML,/WEB-INF/service. xml </param-value>
</Context-param>
<Servlet>
<Servlet-Name> context </servlet-Name>
<Servlet-class>
Org. springframework. Web. Context. contextloaderservlet
</Servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
3. Use contextloaderplugin to create applicationcontext.
Spring provides the contextloaderplugin class for loading configuration files at startup. The default configuration file loaded by contextloaderplugin is the servletName-servlet.xml, where servletname is the servlet name corresponding to the struts actionservlet. For example, the following definitions are made in Web. xml:
<Servlet>
<Servlet-Name> action </servlet-Name>
<Servlet-class>
Org. Apache. Struts. Action. actionservlet
</Servlet-class>
</Servlet>
Contextloaderplugin loads/WEB-INF/action-servlet.xml by default, which serves as the configuration file for spring. Therefore, if spring has only one configuration file and the file name is action-sevlet.xml, you just need to add the following code in the struts-config.xml configuration file:
<Plug-in classname = "org. springframework.
Web. Struts. contextloaderplugin "/>
If multiple configuration files exist, use the <set-Property = "" value = ""/> element to load multiple configuration files. If multiple configuration files are loaded, add the following code in the struts-config.xml file:
<Plug-in
Classname = "org. springframework. Web. Struts. contextloaderplugin">
<Set-Property = "contextconfiglocation"
<! -You can load multiple configuration files separated by commas (,), spaces, or semicolons. -->
Value = "/WEB-INF/action-servlet.xml,
/WEB-INF/applicationcontext. xml "/>
</Plug-in>
Address: http://blog.csdn.net/wuxiaohong520/archive/2007/10/04/1811325.aspx