`

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 + struts 整合的三种主要方式

    ### Spring与Struts整合的三种主要方式 在Java Web开发领域,Spring框架和Struts框架都是非常重要的技术。Spring框架以其强大的依赖注入(DI)和面向切面编程(AOP)功能,为Java应用提供了轻量级的解决方案。而...

    Spring整合Struts

    另一种整合方式是通过继承Spring的ActionSupport类,这使得开发者能够显式地使用getWebApplicationContext()方法获取Spring管理的bean。这种方式更适用于那些需要深度集成Spring特性的场景,比如AOP或更复杂的依赖...

    struts+spring+hibernate三大框架整合

    Spring整合Struts主要有三种方式: 1. **使用Spring的ActionSupport**:Action类直接继承自Spring的ActionSupport,通过`super.getWebApplicationContext()`获取Spring上下文,然后通过`ApplicationContext.getBean...

    struts2+spring整合

    Struts2和Spring的整合是Java企业级开发中常见的技术结合,主要目的是为了利用Spring的强大IoC(控制反转)和AOP(面向切面编程)功能,以及Struts2的优秀MVC架构来构建可维护性和扩展性更强的Web应用。下面我们将...

    struts 整合spring 例子,测试通过

    Struts2 和 Spring 整合是Java开发中常见的实践,主要目的是为了利用Spring的强大功能,如依赖注入(DI)和面向切面编程(AOP),同时保持Struts2的MVC架构的优势。以下是对整合过程的详细说明: 首先,Spring框架...

    Spring 和 struts 整合的三种方式

    总结来说,Spring和Struts的整合提供了多种选择,可以根据项目的具体需求和团队的技术栈来选择合适的方式。第一种方式简单易行,但Action与Spring有一定耦合;第二种方式降低了耦合,但需要自定义RequestProcessor;...

    struts2整合spring

    3. **Struts2-Spring插件**:`struts2-spring-plugin-x.xx.jar`,这是实现Struts2与Spring整合的关键组件。 此外,还需要其他第三方库的支持,例如`commons-logging.jar`、`ognl-x.xx.jar`等。 #### 三、配置...

    SSH整合 struts+hibernate+spring

    - **Struts与Spring整合的注意事项**: - 避免直接在Action类中访问Spring上下文,以免破坏Spring的封装性。 - 对Struts的定制可以通过自定义Action代理或者拦截器来实现。 通过以上几个方面的介绍,可以看出SSH...

    spring 整合 struts 文档

    通过上述三种不同的整合方式,我们可以根据项目的具体需求选择最合适的方法来实现Spring和Struts框架的有效集成。每种方法都有其特点和适用场景,理解它们的工作原理可以帮助我们更好地设计和开发出高效、灵活的Java...

    Spring与Struts整合3种方式实例

    整合 Spring 和 Struts 可以使得应用的业务逻辑层更好地管理和控制,提高代码的可测试性和可维护性。以下是三种整合 Spring 与 Struts 的方法的详细解释: 1. 通过 Spring 的 `ActionSupport` 类 这种方法是让 ...

    struts+habernate+spring整合实例源代码

    在“struts+habernate+spring整合实例”中,这三个框架协同工作,实现了高效、灵活的业务逻辑处理。具体步骤可能包括: 1. **配置环境**:首先,需要在项目中引入这三个框架的库,并配置相关的XML文件,如struts-...

    Struts,Hibernate和Spring三个框架整合的经典登陆小程序

    Struts、Hibernate和Spring是Java开发中非常著名的三大框架,它们各自解决应用程序的不同层次问题,而将它们整合在一起,能够构建出高效、可维护的Web应用程序。本项目是一个基于这三个框架实现的简单登录功能,旨在...

    spring与struts2整合

    Spring 和 Struts2 整合是企业级 Java 开发中常见的技术组合,这两种框架的结合可以充分利用它们的优点,提供灵活的控制层和强大的持久化支持。下面将详细解释整合过程中的关键知识点。 首先,整合所需的基础组件...

    Struts+Spring+Hibernate 简单例子开发

    本案例通过整合Struts、Spring以及Hibernate三大框架来实现一个简单的应用开发,旨在演示如何利用这三种技术来构建一个基本的应用程序。该应用程序的核心功能是通过用户输入ID来查询并展示相关信息(例如用户名和...

    用 Spring 更好地处理 Struts 动作三种整合

    总的来说,Spring 和 Struts 的整合允许开发团队利用两者的优点,例如 Struts 的 MVC 简单性和 Spring 的强大依赖管理和事务处理能力。通过适当的整合,可以构建出既高效又可维护的 Java Web 应用程序。对于那些已经...

    Eclipse + MyEclipse整合Struts+Spring+Hibernate简单例子开发

    ### Eclipse + MyEclipse 整合 Struts+Spring+Hibernate 开发示例 #### 一、项目背景与目标 本文档旨在通过一个简单的实例来演示如何利用Eclipse结合MyEclipse进行Struts、Spring和Hibernate(SSH)框架的集成开发...

    spring4.1核心包

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

    Struts1.3+Spring3.0+Hibernate3.3+DWR2.X框架集成文档v1.4

    #### 整合Spring与Struts ##### 1. 在web.xml中进行Struts和Spring的配置 为了实现Struts与Spring框架的集成,在`web.xml`文件中需要进行以下配置: - **Struts配置**: - 配置`ActionServlet`以定义Struts的...

    Spring集成Struts、Hibernate.pdf

    #### 一、Spring与Struts整合背景及意义 Spring框架作为一款轻量级的Java EE开发框架,在企业级应用开发中占据了举足轻重的地位。它以其强大的依赖注入(DI)和面向切面编程(AOP)能力著称,能够有效地管理和组织...

Global site tag (gtag.js) - Google Analytics