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

分类:Java 2010-04-25 来源:CodeWeblog.com 人气:353

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:)

分享到:
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 name

  • 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 descripto

  • 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

  • 解析web.xml中在Servlet中获取context-param和init-param内的参数 2013-11-20

    本篇文章是对web.xml中在Servlet中获取context-param和init-param内的参数进行了详细的分析介绍,需要的朋友参考下 web.xml里面可以定义两种参数:1.application范围内的参数,存放在servletcontext中,在web.xml中配置如下: <context-param> <param-name>context/param</param-name> <param-value>avalible duri

  • Access to context-param in web.xml and init-param parameter 2010-03-19

    web.xml which can be defined two kinds of parameters: (1) application within the parameters stored in the servletcontext, in the web.xml to configure the following: xml code <context-param> <param-name> context / param </ param-name> ...

  • web.xmlweb.xml configuration Xiangjie 2010-08-05

    Web.xml common elements <web-app> <display-name> </ display-name> defines the name of WEB applications <description> </ description> Statement WEB application descriptions <context-param> </ context-param> context

  • 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 ex

  • The first struts2 web application 2010-11-04

    One. Software Configuration Eclipse 3.4.1 JDK 1.6.0_10 Tomcat 6.0 com.sysdeo.eclipse.tomcat_3.2.1 plug-in (Copy needed to eclipse \ plugins directory, if you do not take effect, you can try using eclipse.exe-clean start) II. Struts2 related configuration

  • 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, Tomc

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

processed in 0.361 (s). 12 q(s)