spring in the web.xml and in the struts of different configurations

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>
</ context-param>

<listener-class> org.springframework.web.context.ContextLoaderListener </ listener-class>
</ listener>

<servlet-name> context </ servlet-name>
<servlet-class> org.springframework.web.context.ContextLoaderServlet </ servlet-class>
<load-on-startup> 1 </ load-on-startup>
</ servlet>
- "

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" />
</ action>
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">
<property name="businessService">
<ref bean="businessService"/>
</ property>
</ bean>

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
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml" />
</ plug-in>
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:)

分类:Java 时间:2010-04-25 人气:355
blog comments powered by Disqus


  • Tomcat Web application using web.xml control Xiangjie (2) 2010-07-30

    Tomcat Web application using web.xml control Xiangjie (2) Study: feng_sundy Source: unknown (2006-04-29 13:42:24) 7 specified welcome page If the user provides a http://host/webAppPrefix/directoryName/ such as a directory name that contains the file

  • Tomcat Web application using web.xml control Xiangjie (1) 2010-07-30

    Transfer from: http://fanqiang.chinaunix.net/app/web/2006-04-29/4122.shtml Tomcat Web application using web.xml control Xiangjie (1) Study: feng_sundy Source: unknown (2006-04-29 13:39:34) A definition of the head and the root element Deployment desc

  • Web application firewall? Web Security Gateway? 2010-11-14

    Original http://netsecurity.51cto.com/art/201007/210932.htm Whether it is Web-based application system or a Web site, they are faced with a variety of security threats and sources of uncertainty. Some of them have been found, and has an identifiable

  • Web application firewall? Web Security Gateway? (Continued) 2010-11-14

    Original http://netsecurity.51cto.com/art/201007/210932_3.htm System management SecureSphere provides fully distributed three-tier network architecture, including: the first layer - the business and implementation of detection engine, the second laye

  • Detailed one JSP related technologies (web applications and web.xml files) 2010-10-26

    Web application's file structure is as follows: |-WEB-INF | |-Classes | |-Lib | |-Web.xml | -*. Jsp (that holds the number of Jsp page) Web.xml configuration descriptor In each Web application path of the WEB-INF \ down and conf \ Web.xml under the e

  • Tomcat web application deployment 2010-11-06

    Max on us today about web training program in the proper deployment under tomcat. Mainly include: 1.java the classloader in tomcat architecture under http://www.jaxmao.org/tomcat-docs/class-loader-howto.html Wrote Like many server applications, like,

  • Common Web application security issues (1 - 4) (a) 2010-05-31

    After the two ("Web security issues of hierarchy" and "Understanding the essence of Web application security issues") on the Web security and Web application security conceptual knowledge of the macroscopic description, I believe we ha

  • Spring source code analysis (2): IoC containers in the Web container to start 2009-03-06

    Above, we have analyzed the implementation of IOC container itself, let's look at a typical web environment, Spring IOC container is what is loaded and functional. Simply put, in the web container, through the ServletContext for the Spring's IOC cont

  • Defined in server.xml a new WEB application 2010-05-26

    Modify server.xml, define a new WEB application The specific path to server.xml file as follows: D: \ Program Files \ Apache Software Foundation \ Tomcat 5.0 \ conf \ server.xml. The server.xml file the most is not the end </ Host> key insert a <

  • WEB application of the servlet's service, doPost, doGet method of the relationship between the 2010-03-29

    WEB application of the servlet's service, doPost, doGet method of the relationship between the Servlet's life cycle: Including the loading and instantiating, initializing, processing requests and service end. The survival period from javax.servlet.Se

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.388 (s). 10 q(s)