详细分析spring与struts2的整合原理

本文详细介绍了Spring与Struts2的整合过程,包括导入相关包、配置Spring的IOC容器以管理Struts2的Action,并探讨了整合原理。在整合中,需要注意Spring的bean默认是单例,而Struts2的Action可能是多例。通过struts-plugin.xml文件,Struts2在启动时会从IOC容器中获取Action实例。文章还深入研究了Struts2的静态注入和自定义对象工厂,以及如何通过配置覆盖默认行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring与struts2如何整合:

  • 1). 整合目标 ? 使 IOC 容器来管理 Struts2 的 Action!
  • 2). 如何进行整合 ?
1. 正常导入入 Struts2与spring包之后 还需要一个额外的包:
struts2-spring-plugin-2.3.3.jar
2 .在 Spring 的 IOC 容器中配置 Struts2 的 Action.即在 IOC 容器中配置 Struts2 的 Action。

这时注意一个问题:spring IOC容器中的bean默认是单例的,而如果struts2的Action是多例的,在装配Action时注意设置scope。
修改web.xml为:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:Spring/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

这样在声明Action实例时只需要指向在IOC容器中的id即可。
如上就是Spring与Struts2的整合,非常简单。现在我们来分析原理。

spring与struts2整合原理:

通过添加 struts2-spring-plugin-2.3.15.3.jar 以后, Struts2 会先从 IOC 容器中获取 Action 的实例.

首先从struts入手,了解struts的插件机制。

Struts-plugin.xml
说明:
Struts-plugin.xml文件是在tomcat服务器启动的时候加载的
该配置文件在classpath的根目录下
在每一个含有plugin字母的jar包的根目录下有一个struts-plugin.xml文件 (例如 刚刚我们导入的struts2-spring-plugin)
当tomcat启动的时候,就把所有的含有plungin的jar包的该配置文件就加载了。

研究struts2的静态注入:

1、 在struts2的配置文件struts-default中写的bean:

这里写图片描述

在tomcat启动的时候,会执行OgnlValueStackFactory的createValueStack方法

这里写图片描述

从上图可以看出,在tomcat启动的时候就决定了ValueStack的实现类.

2、 改变静态注入的方式

可以在struts.xml文件中对struts-default.xml文件中的bean进行配置和覆盖,这样就可以按照覆盖掉的执行。在struts.xml文件中,做如下的配置:

这里写图片描述

这个时候,当tomcat启动的时候,将会报错。

这里写图片描述

了解了struts2的静态注入后,我们来改变action的产生方式。

struts有一个类:ObjectFactory

这里写图片描述

说明:
Struts2容器就是利用ObjectFactory的上面的几个方法产生的action,result,interceptor.

struts2的配置文件struts-default.xml中:

这里写图片描述

在struts2容器中,对象工厂是由StrutsObjectFactory来产生的。

这里写图片描述

从上图中可以看出,StrutsObjectFactory继承了ObjectFacotry,但是没有覆盖buildAction方法,说明struts2的action的产生还是调用了ObjectFactory中的buildAction方法。

写一个对象工厂:

public class Itheima09ObjectFactory  extends ObjectFactory{
  @Override
public Object buildAction(String actionName, String namespace, ActionConfig config,
        Map<String, Object> extraContext) throws Exception {
    // TODO Auto-generated method stub
        System.out.println("create Action");
        return "66";
}
}

该类重写了buildAction方法。
配置文件:

这里写图片描述

说明:
当在浏览器提交一个请求,请求某一个action的时候,在创建action的时候,就会进入到Itheima09ObjectFactory中执行buildAction方法。

现在我们回过头看spring与struts的整合使用的jar包:

这里写图片描述

Struts-plugin.xml:

这里写图片描述

说明:对象工厂为spring:StrutsSpringObjectFactory。

总结

当tomcat启动的时候,加载了很多个struts-plugin.xml文件,在该文件中可以配置
Bean
Package
Interceptor
Action
Result
这些元素就被加载到了struts2容器中。
如果不想要某一些内容,只要把这些内容所在的struts-plugin.xml的jar包去掉就可以了。
插件的主要体现形式是jar包。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值