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

sponsored links
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:
<context-param>
<param-name> contextConfigLocation </ param-name>
<param-value> / WEB-INF/applicationContext.xml </ param-value>
</ context-param>

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

<! - OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER
<servlet>
<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:)
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

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

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • myeclipse plugin

    myeclipsePlug-ins? 1.tomcatPlugin(Start tomcat ):http ://www.sysdeo.com/eclipse/tomcatPlugin.html,2.xVersions of eclipse 3 version 2 .1Version doesn't work. 2.Lomboz(Development of jsp program ,jspDynamic prompt, debugging ):http://forge.objectweb.org/pro

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries