【WEB】struts2整合spring原理以及源码剖析

一、原理概述

1. struts和spring整合,到底是谁整合谁?

        是struts整合spring还是spring整合struts?这个问题重要吗?有必要讨论吗?

        说struts整合spring,是因为struts比spring晚出现,所以只能是struts整合spring,貌似有一定的道理,但是不绝对正确。一个框架的流行取决于市场的接受程度,在struts很流行的前提下如果spring没有那么流行,那么无论是谁先出现谁后出现,恐怕spring为了自身的发展也会要去整合struts。

        事实上的确是struts整合spring,但原因不是两个框剪出现的时间,而是两个框架提供了什么样的jar包。struts和spring整合后的运行环境不再只是单纯的JVM,而是WEB服务器,spring提供接入web的jar包是spring-web-x.x.x.RELEASE.jar,这个jar包的作用是将spring容器整合到WEB容器(见上一篇博文:《【Spring】Spring&WEB整合原理及源码剖析》),而struts提供的jar包是struts2-spring-plugin-x.x.x.x.jar,这个jar包里面改写了struts默认的对象工厂ObjectFactory,重新提供了一个新的对象工厂SpringObjectFactory,在这个工厂类里面你会看到出现spring框架中的类和对象,也就是struts通过这个jar包,把spring整合到了自己的框架里面,从而享受spring带来的便利。

2. struts是表现层MVC框架,而spring是业务层框架,struts整合spring,整合的到底是什么东西?

        spring的核心是IOC和AOP(加多spring MVC和spring JDBC/事务),通俗的讲spring是框架的框架,他自己本身就是一个框架,但是他主要服务于其他框架。所以整合的核心就落点于spring的”服务“,spring为其他框架提供了什么样的服务?换言之,struts需要把什么东西托管给spring,从而获得更好的服务?

        这一切谜题的答案就在struts框架提供整合spring的jar包之中:struts2-spring-plugin-x.x.x.x.jar。

        这个jar包里面主要提供了SpringObjectFactory这个对象工厂,将原来struts对象工厂创建的实例交由spring IOC容器来创建和管理,从而首先享受了spring提供的IOC服务,因为从spring容器获得bean实例,从而又享受到了spring带来的其他诸如AOP、spring JDBC、spring事务管理等等服务。

        所以,struts整合spring,是将spring的IOC整合到了自己的框架里面!

3. 整合步骤如概述

        1. spring整合web,参看上一篇文章《【Spring】Spring&WEB整合原理及源码剖析》,主要由spring框架提供的jar包完成:spring-web-x.x.x.RELEASE.jar;

        2. struts接入spring,将原来struts自己ObjectFactory创建的对象交由SpringObjectFactory来创建并缓存,主要由struts提供的jar包完成:struts2-spring-plugin-2.3.15.3.jar;

        3. 最后,不要忘记struts的Action托管给spring,并且设置scope="ptototype";

        

二、实现细节&源码code实现

        一句话struts整合spring:配置好web.xml(spring接入web,struts配置过滤器,剩下的交由两个jar包处理即可,是不是太TM的简单了?一切就是那么简单!),导入spring-web-x.x.x.RELEASE.jar,导入struts2-spring-plugin-2.3.15.3.jar,完成!

        因为spring整合web上一次有过详细讨论,这一次主要讨论的是struts提供的jar包,也即struts对象工厂的替换。

1. 怎么实现工厂替换

        struts默认的对象工厂是ObjectFactory,该工厂提供了在struts启动的时候实例化、装配bean的操作,主要是struts-default.xml中配好的bean/result/interceptor(缓存在ContainerImpl中),以及运行期的action及其参数注入。

        SpringObjectFactory是ObjectFactory的子类,要想替换ObjectFactory,利用面向对象三大特性“多态”即可完成。要想完成替换,需要告知struts你的意图,替换的开关在最先加载的框架起配置文件default.properties中,即struts.objectFactory = spring。要想打开这个开关,你可以在struts-default.xml、struts-plugin.xml、struts.xml、struts.properties、web.xml总共五处完成操作,当然struts-default.xml和struts-plugin.xml是不能修改的,除非系统已经提供。毋庸置疑,既然struts整合spring,那么struts提供的整合插件必然打开了这个开关,不需要你再手动配置。

        struts.xml中对象工厂替换配置内容,默认开关是关闭的。

### if specified, the default object factory can be overridden here
### Note: short-hand notation is supported in some cases, such as "spring"
###       Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here
# struts.objectFactory = spring(看我看我看我看我看我,我就是开关,默认是关闭的,已经在struts-spring-plugin-xxx.jar中打开了,引入了该jar包即可,剩下的什么也不用操作)

### specifies the autoWiring logic when using the SpringObjectFactory.
### valid values are: name, type, auto, and constructor (name is the default)
struts.objectFactory.spring.autoWire = name

### indicates to the struts-spring integration if Class instances should be cached
### this should, until a future Spring release makes it possible, be left as true
### unless you know exactly what you are doing!
### valid values are: true, false (true is the default)
struts.objectFactory.spring.useClassCache = true

### ensures the autowire strategy is always respected.
### valid values are: true, false (false is the default)
struts.objectFactory.spring.autoWire.alwaysRespect = false
        struts-plugin.xml,struts提供的整合jar包配置文件内容,可以看到开关已经打开, <constant name="struts.objectFactory" value="spring" />。

<?xml version="1.0" encoding="UTF-8" ?>
<!--
/*
 * $Id: struts-plugin.xml 1221225 2011-12-20 12:22:28Z jogep $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for addition
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值