When I first came to this company for nearly half a month, looked at 10 years of experience developed in java to write the ssh master frame, feel and their own at school and really do not like this is really the framework of E Haha ssh Hugong a beginning not to understand why the bar, spring loading it into web.xml been carried out, rather than the book that as a plugin in the struts Riga it, and he is quite puzzled, and today finally the Internet to find the article again, I finally resolved the doubt in his heart. A ~ ~ ~ happy ~ ~ A lot of long experience in Afghanistan
Spring in the WebApplicationContext, Research
ApplicationContext is the Spring core, Context context, we usually interpreted as the environment, I would like to use "container" to express it more easier to understand some, ApplicationContext is "application container" of: P, Spring put Bean on the container, in the when necessary, with the getBean method of removal, although I have not read this part of the source code, but I think it should be a similar to the Map structure.
At the Web application, we will use WebApplicationContext, WebApplicationContext inherited from the ApplicationContext, let us look at the Web application, how to initialize WebApplicationContext, defined in the web.xml:
<param-name> contextConfigLocation </ param-name>
<param-value> / WEB-INF/applicationContext.xml </ param-value>
<listener-class> org.springframework.web.context.ContextLoaderListener </ listener-class>
<! - OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER
<servlet-name> context </ servlet-name>
<servlet-class> org.springframework.web.context.ContextLoaderServlet </ servlet-class>
<load-on-startup> 1 </ load-on-startup>
As can be seen, there are two methods, one is to use ContextLoaderListener this Listerner, the other is ContextLoaderServlet this Servlet, these two methods are in the web application startup time to initialize WebApplicationContext, I personally think that Listerner better than Servlet some, because Listerner start and end monitor applications, while the Servlet may start to be a slight delay in some, if at this time to do some business operations to start before and after the order is influential.
So in the end of the ContextLoaderListener and ContextLoaderServlet done anything at all?
To ContextLoaderListener example, we can see that
public void contextInitialized (ServletContextEvent event) (
this.contextLoader = createContextLoader ();
this.contextLoader.initWebApplicationContext (event.getServletContext ());
protected ContextLoader createContextLoader () (
return new ContextLoader ();
ContextLoader is a utility class used to initialize the WebApplicationContext, the main way is to initWebApplicationContext, we continue to track the initWebApplicationContext this method (specifically, the code I do not put up, we can look at Spring's source code), we found that the original ContextLoader is to WebApplicationContext (XmlWebApplicationContext is the default implementation class) on the ServletContext in, ServletContext is a "container" is also a structure similar to the Map, while the WebApplicationContext in the ServletContext of the KEY is WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, we will need if you want to use the WebApplicationContext from the ServletContext removed Spring provides a WebApplicationContextUtils class, you can easily remove the WebApplicationContext, as long as the ServletContext passed on it.
Above, we introduced the WebApplicationContext is initialized in the Servlet container principle, the general Web applications can be easily used, but, with the extensive use of Struts, the Struts and the Spring as a whole together is a need to face the problem Spring itself provides a Struts-related classes are mainly used org.springframework.web.struts.ActionSupport, as long as we own Action inherited from ActionSupport, is that you can call ActionSupport in getWebApplicationContext () method out WebApplicationContext, but this one years in Action, the need to obtain a place of business logic should be getBean, seems simple enough, so Spring also provides another method, org.springframework.web.struts.ContextLoaderPlugIn, this is a Struts-Plug, to start in the Struts When loaded, for Action, can manage, like Bean to manage, in the Action in struts-config.xml configuration looks something like the following
<action attribute="aForm" name="aForm" path="/aAction" scope="request" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="forward" path="forward.jsp" />
Note that type into a org.springframework.web.struts.DelegatingActionProxy, then we need to create action-servlet.xml such a document, action-servlet.xml comply with Spring's spring-beans.dtd standards, which define similar to the following
<bean name="/aAction" singleton="false">
com.web.action.Aaction is the Action implementation class, businessService is necessary business logic, Spring will businessService injected into the Action, in the Action long as you write businessService the get and set methods can be, there is still a little, action of the bean is a singleton = "false", that is, create a new instance each time, this also solved the Struts in Action thread synchronization problems, specifically the process is when the user do "/ aAction" of the HTTP request (of course, should be "/ aAction.do" ), Struts will find a counterpart of this Action class org.springframework.web.struts.DelegatingActionProxy, DelegatingActionProxy is a proxy class, it will look for action-servlet.xml file "/ aAction" corresponds to the real implementation class, and then put it an instance of , along with the needed injection of business objects, and then implementation of the Action's execute method.
Used ContextLoaderPlugIn, in the struts-config.xml like this into the configuration
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml" />
Is no longer needed in the web.xml in the ContextLoaderListener or ContextLoaderServlet.
Having said that do not know if you will not have this problem, if you use ContextLoaderPlugIn, if some of our program is out of Struts in Action environment, how do we deal with, for example we have to custom tag libraries, in the tag library, we need to call the Spring management of the business logic layer object, this time we are very troublesome, because only in the action in the dynamic business logic into the other we seem to not get Spring's WebApplicationContext.
Do not worry, we still look ContextLoaderPlugIn source code (source code is no longer posted), we can see, the original ContextLoaderPlugIn is still on the ServletContext in the WebApplicationContext, but it is KEY not the same, and the KEY value ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + ModuleConfig.getPrefix () (specifically, please see the source code), This is just great, and we know WebApplicationContext on where Web applications, as long as we can get to the ServletContext also be able to get to the WebApplicationContext in:)