With spring web apps, instead of creating a spring container manually, you create a spring container declaratively through a configuration file, so there are two ways to create a spring container in a web app:
I. Configuring the spring container directly in the Web. xml file
Two. Create a spring container with the extension points of the third-party MVC framework
In fact, the first way is most common.
In order for the spring container to start automatically with the application of the Web, there are two ways
1. Using Servletcontextlistener to achieve
2. Implemented with Load-on-startup servlet
For the use of Servletcontextlistener implementation, the operation and description are as follows
Spring provides an implementation class Contextloadlistener for Servletcontextlistener, which can be used as listener and is automatically found at creation time web-inf/ Applicationcontext.xml file, so if there is only one configuration file and the name is Applicationcontext.xml, simply add the following configuration to the Web. xml file
[HTML] view plain copy print? <listener> <listener-class> Org.springframework.web.context.ContextLoaderLister </listener- Class> </listener> If more than one configuration file needs to be loaded, consider using the <context-param> element to determine the file name of the configuration file. When Contextloadlistenter loads, it looks for a parameter named Contextconfiglocation. Therefore, when configuring Context-param, the parameter name should be contextconfiglocation
The Web. xml file with multiple configuration files is as follows
[HTML] View plain copy print? <?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance " xsi:schemalocation=" http://java.sun.com/xml/ns/j2ee http ://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> < context-param> <!-- parameter named contextconfiglocation --> <param-name>contextConfigLocation</param-name> <!-- Configure multiple files, Separated by "," --> <param-value>/web-inf/actioncontext.xml,/web-inf/ appcontext.xml,/web-inf/daocontext.xml</param-value> </context-param> <!-- Creating ApplicationContext instances with listener --> <listener> <listener-class> org.springframework.web.context.contextloaderlister </listener-class> </listener> </web-app>
If the configuration file is not specified by Contextconfiglocation, Spring automatically finds the Applicationcontext.xml file and, if there is a contextconfiglocation, uses the configuration file determined by this parameter. If a suitable configuration file cannot be found, spring will not initialize properly
Spring creates Webapplicationcontext objects based on the bean definition and saves them in the ServletContext of the Web App. In most cases, the applied bean does not need to feel the presence of the ApplicationContext, as long as the ApplicationContext IOC container is used
If you need to get an ApplicationContext instance in your app, you can get it by using the following code
Gets the spring container for the current web App
Webapplicationcontext Ctx=webapplicationcontextutils.getwebapplicationcontext (ServletContext);
Two. Create a spring container using the extension points of the third-party MVC framework
struts have an extension point plugin,spring is using plugin as an extension point, This provides the integration with struts. Spring provides the plugin implementation class Org.springframework.web.struts.ContextLoadPlugIn, which can be used as the plugin configuration of struts, when the struts framework starts, The spring container is automatically created
in order to create a spring container with struts's plugin, you only need to add the following fragment to the Struts configuration file Struts-config.xml
<plug-in classname= "Org.springframework.web.struts.ContextLoaderPlugIn";
< Set-property property= "contextconfiglocation" value= "/web-inf/actioncontext.xml,/web-inf/appcontext.xml,/web-inf /daocontext.xml "/>
</plug-in>
When you specify the Contextconfiglocation property value, you can specify the location of a spring configuration file, and you can specify the location of multiple spring configuration files