Struts1.X与Spring集成——另一种方案

版权声明:本文为博主原创文章。未经博主赞成不得转载。

https://blog.csdn.net/hanxuemin12345/article/details/38070063 css

上篇博客介绍了Struts1.XSpring集成的一种方案。html

Struts1.X与Spring集成——第一种方案java

此篇博客还以上篇博客的登陆样例为例,介绍Struts1.XSpring集成的还有一种方案。node

1,第一种方案 原理

回顾第一种方案集成原理:在Action中取得BeanFactory,经过BeanFactory取得业务逻辑对象web

此种方案的缺点:从严格意义的分层上来看,Action上看到了Spring的相关东西。依赖Spring API去查找东西。发生了依赖查找。因为要查找依赖对象,因此要依赖Spring服务才干找到,因为在Spring提供的工厂里。spring

应该是Action中看不到Spring相关东西。Action中看到的就是业务接口,这种话层次更加分明。apache

2。另一种方案 原理

基于第一种方案的缺点。咱们改为不在去查找业务对象。让IOC注入进来,仅仅要提供setter方法,就能把对象主动传过来——依赖注入(局限性:在同一个JVM里可以,跨JVM不可以依赖注入)app

 

假设想被Spring注入,Action需要被Spring管理。也就是说LoginAction的建立是从IOC容器中建立出来的。而不该该再是让Struts建立。假设仍是Struts建立的话,UserManager是不可能注入进来的。jsp

仅仅有从IOC容器中拿出来的东西,所有的依赖关系才被注入,因此Action必须被Spring管理。被Spring管理,就需要配置到Spring的配置文件里。post

 

applicationContext-beans.xml


 

applicationContext-action.xml:


 

仅仅要经过BeanFactory getBean()"LoginAction"拿到,同一时候userManager的依赖关系都会注入进去;

 3,分析另一种方案提出的起因

第一种方案:


 

Webclient发出请求,请求ActionServletStruts流程是在Struts里面要查看Struts-config.xml,LoginAction拿出来new。例如如下:


Struts进行new,仅仅会new LoginAction。其它的UserManager不会new,也不会注入。

因此此种流程是不行的,依赖对象不能注入。

但是,用Struts的话必须得请求Action。因此需要使用SpringSpring实现了一个ActionAction代理类:ActionProxy)——不会改变原先Action的相关接口,仅仅是代理,代理可以控制原目标。在调目标以前可以作些事情。因此Struts-config.xml文件里就不该该配置LoginAction.java这个类了,要配置Spring给提供的一个类,它提供的类起到了什么做用:webclient发出请求,需要StrutsSpring提供的代理类new出。这个代理类中负责拿到BeanFactory,经过getBean()。把此次请求相应的Action拿出来(是从IOC容器中拿出LoginAction),那么它的依赖对象就都会被注入。

例如如下图另一种方案:

另一种方案:


将第一种方案,Struts-config.xml中配置的LoginAction.Java(例如如下图(1))改成配置org.springframework.web.struts.DelegatingActionProxy.class(2)

(1)


(2)


 

(3)

applicationContext-action.xml:配置LoginAction类,来管理LoginAction


 

一个请求过来,Strutsnew出代理类DelegatingActionProxy,经过代理类调用getBean(beanName,Action.Class)方法,将path的名称"/login"传入,到IOC中找到相应nameAction(即,LoginAction(applicationContext-action.xml中配置过了,如图(3))

 

另一种方案集成原理:StrutsAction交给Spring建立。

将业务逻辑对象经过spring注入到Action中,从而避免了在Action类中的直接代码查询

(client请求---->代理action--->取得BeanFactory--->getBean(..)建立action演示样例--->执行exctute方法)

4。代码演示样例

简单登陆样例

仅仅是Spring配置文件和Struts配置文件。以及LoginAction.java有所变更,其它代码參考 Struts1.X与Spring集成——第一种方案

struts-config.xml配置文件:配置代理Action及ActionForm

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
<!--生命ActionForm(即,告诉Struts哪一个是ActionForm)  -->
    <form-beans>
       <form-bean name="loginForm" type="com.bjpowernode.usermgr.web.forms.loginActionForm"/>
    </form-beans>
 
<!-- 描写叙述Action -->
   <!-- 描写叙述Action -->
    <action-mappings>
        <action path="/login"
                type="org.springframework.web.struts.DelegatingActionProxy"
                name="loginForm"
                scope="request">   
                
        <!-- 转向信息 -->       
        <forward name="success" path="/login_success.jsp"/>
        </action>
    </action-mappings>
    <message-resources parameter="MessageResources" />
</struts-config>


applicationContext-beans.xml配置文件:配置业务逻辑对象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	<!-- 这个IOC容器核心是个工厂,抽象工厂 -->
	<bean id="userManager" class="com.bjpowernode.usermgr.manager.UserMangerImpl"/>
    
</beans>

applicationContext-action.xml配置文件:配置Struts的Action

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	<!-- 配置Struts的Action -->
	<bean name="/login" class="com.bjpowernode.usermgr.web.actions.LoginAction" scope="prototype">
       <property name="userManager" ref="userManager"/>
   </bean>
</beans>

5,执行

參考 Struts1.X与Spring集成——第一种方案


6,总结

第一种方案原理:Action中取得BeanFactory,经过BeanFactory取得业务逻辑对象——依赖查找;
另一种方案原理:将Struts的Action交给Spring建立,这样业务逻辑对象将会被注入——依赖注入;

二者差异及优缺点:
第一种是依赖查找因为要查找依赖对象,因此要依赖Spring服务才干找到。这样就依赖了Spring服务。假设脱离了Spring,Action对象不能单独使用。
另一种是依赖注入,不依赖于别的服务。这样就避免了依赖查找,不用关系其它服务是怎样工做的,没有侵入性,从而也使代码很是清晰。
相关文章
相关标签/搜索