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


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

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

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

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

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

    1,第一种方案 原理

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

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

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

    2。另外一种方案 原理

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

     

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

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

     

    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对象不能单独使用。
    另外一种是依赖注入,不依赖于别的服务。这样就避免了依赖查找,不用关系其它服务是怎样工作的,没有侵入性,从而也使代码非常清晰。




     

  • 相关阅读:
    chrome 开发者工具——前端实用功能总结
    而立之年——回顾我的前端转行之路
    编译原理实战入门:用 JavaScript 写一个简单的四则运算编译器(修订版)
    手把手带你入门前端工程化——超详细教程
    手把手教你搭建 Vue 服务端渲染项目
    前端项目自动化部署——超详细教程(Jenkins、Github Actions)
    前端国际化辅助工具——自动替换中文并翻译
    深入了解 webpack 模块加载原理
    实现一个 webpack loader 和 webpack plugin
    博客本地编辑器-OpenLiveWriter安装使用
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10972913.html
Copyright © 2020-2023  润新知