`

struts和spring的整合-WebApplicationContext的取得方式

阅读更多

根据不同的整合方式,分别看一下WebApplicationContext的取得方式

 

1>业务Action extends ActionSupport

 

ActionSupport.java

protected final WebApplicationContext getWebApplicationContext() {
	return this.webApplicationContext;
}

protected WebApplicationContext initWebApplicationContext(ActionServlet actionServlet)
			throws IllegalStateException {

	return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, null);
}

public void setServlet(ActionServlet actionServlet) {
		super.setServlet(actionServlet);
		if (actionServlet != null) {
			this.webApplicationContext = initWebApplicationContext(actionServlet);
			this.messageSourceAccessor = new MessageSourceAccessor(this.webApplicationContext);
			onInit();
		}
		else {
			onDestroy();
		}
}

 

如果WebApplicationContext是通过struts plugin的方式加载的话,会直接获取default module的wac

 

2>使用DelegatingRequestProcessor

DelegatingRequestProcessor.java

private WebApplicationContext webApplicationContext;
	

public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException {
		super.init(actionServlet, moduleConfig);
		if (actionServlet != null) {
			this.webApplicationContext = initWebApplicationContext(actionServlet, moduleConfig);
		}
}

protected WebApplicationContext initWebApplicationContext(
			ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException {

		return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
}

protected final WebApplicationContext getWebApplicationContext() {
		return this.webApplicationContext;
}

 

 

3>所有的Action类都使用DelegatingActionProxy

DelegatingActionProxy.java

 

protected WebApplicationContext getWebApplicationContext(
			ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException {

		return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
}

 

可以发现,这三种整合方式,其实都是通过DelegatingActionUtils.findRequiredWebApplicationContext方法来获取wac的

那么,我们来看一下具体的内部代码

 

public static WebApplicationContext findRequiredWebApplicationContext(
			ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException {

		WebApplicationContext wac = getWebApplicationContext(actionServlet, moduleConfig);
		// If no Struts-specific context found, fall back to root context.
		if (wac == null) {
			wac = WebApplicationContextUtils.getRequiredWebApplicationContext(actionServlet.getServletContext());
		}
		return wac;
}

public static WebApplicationContext getWebApplicationContext(
			ActionServlet actionServlet, ModuleConfig moduleConfig) {

		WebApplicationContext wac = null;
		String modulePrefix = null;

		// Try module-specific attribute.
		if (moduleConfig != null) {
			modulePrefix = moduleConfig.getPrefix();
			wac = (WebApplicationContext) actionServlet.getServletContext().getAttribute(
					ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + modulePrefix);
		}

		// If not found, try attribute for default module.
		if (wac == null && !"".equals(modulePrefix)) {
			wac = (WebApplicationContext) actionServlet.getServletContext().getAttribute(
					ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
		}

		return wac;
}

 

获取逻辑是这样的:

1>先看是否通过struts plugin的方式进行了wac的加载

2>如果没有找到,再接着看是否通过servlet或者listener的方式进行了wac的加载

 

分享到:
评论

相关推荐

    spring-web-5.3.6 jar包.rar

    这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类, 包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 spring的核心类,提供了核心HTTP...

    Struts2+Spring3+MyBatis3完整实例

    - Initializing Struts-Spring integration... - Setting autowire strategy to name - ... initialized Struts-Spring integration successfully - Initializing Spring FrameworkServlet 'dispatcher' - ...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

     这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 (12) spring-webmvc.jar  这个...

    struts-2.3.8+spring-3.2.1+mybatis-3.2.2架构

    最新struts-2.3.8+spring-3.2.1+mybatis-3.2.2架构,包齐全,无冲突,Eclipse开发 导入工程即可 九月 18, 2013 11:39:01 上午 org.apache.catalina.core.AprLifecycleListener init INFO: The APR based Apache ...

    spring jar 包详解

    (11) spring-web.jar 这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、 Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 (12) ...

    spring4.1核心包

    包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 18. spring-webmvc-4.1.1.RELEASE.jar 包含...

    spring+springmvc+mybatis的整合

    这里就要导入mybatis和spring的整合包了,这里sqlsession中也要导入mybatis的配置文件 2.6 spring-bean 配置mapper自动扫描 MapperScannerConfigurer将扫描basePackage所指定的包下的所有接口类(包括子类), ...

    spring-web-2.5.jar

    org.springframework.web.context.WebApplicationContext.class org.springframework.web.context.request.AbstractRequestAttributes.class org.springframework.web.context.request....

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    最新最全的spring开发包

     这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 (12) spring-webmvc.jar 这个...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    Spring MVC之WebApplicationContext_动力节点Java学院整理

    主要介绍了Spring MVC之WebApplicationContext的相关资料,需要的朋友可以参考下

    在web容器(WebApplicationContext)中获取spring中的bean

    Spring把Bean放在这个容器中,普通的类在需要的时候,直接用getBean()方法取出

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    spring-simple-web:使用 Spring Framework 的简单 Web (WAR) 项目

    这个简单的例子演示了在 web 应用程序中使用的 Spring 容器,即org.springframework.web.context.WebApplicationContext Web 应用程序使用 Spring Web 侦听器初始化,例如web.xml org.springframework.web.context...

    开源框架 Spring Gossip

    第一个 Struts 程式 在 Struts 中整合 Spring 第一个 JSF 程式 在 JSF 中整合 Spring <br> 其它 Spring 提供了简化且一致的方式,让您在使用一些 API 或服务时更加简单。 远程...

    Spring.html

    注意:使用注解的方式,最终通知和后置通知顺序换了,建议使用环绕通知 注解 配置 声明式事务管理 PlatFormTransactionManager:平台事务管理器:定义了commit/rollback Mybatis/jdbc:...

    struts2驱动包

    java-struts2.2的驱动包 2009-8-29 14:02:04 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomcat Native library which allows optimal performance in production ...

    spring chm文档

    6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个切入点(pointcut) 6.2.4. 声明通知 6.2.5. 引入(Introductions)...

    Spring TestContext

    Spring TestContext Framework是spring-test模块中提供的一个测试框架,能够用于单元测试和集成测试。

Global site tag (gtag.js) - Google Analytics