zoukankan      html  css  js  c++  java
  • [SSH]struts2-spring-plugin.jar了解

      在struts2-spring-plugin.jar中有一个struts-plugin.xml,里面声明了action类由spring工厂创建。在struts2插件文档里,这样写着“The Spring Plugin works by overriding the Struts ObjectFactory to enhance the creation of core framework objects。” 
        这个插件重写了struts的对象工厂,当创建一个action类时,它会根据struts的配置文件的class属性的值与spring配置文件中的id属性的值相匹配。如果没有与之相匹配,将会像没有使用这个插件前一样创建,然后由spring自动装配。 
        那时我有些不是很明白,为什么我的action类没有写注解@Component(‘xxAction’),还是可以被spring自动装配。那是因为action类被struts和struts2-spring-plugin创建,再由spring自动装配,但不由spring管理。如果我们想使用spring复杂的aop或spring其他的功能时,强烈建议将acion类注册到spring容器中。即让spring去创建action类。 

    假设之前的strust.xml中有如下设置:

    <action name=”CpAction”  class=”cn.ricki.cheung.action.CpAction >
    <result type=”json”/>
    </action>

    现在由Spring来管理对象,在applicationContext.xml添加如下内容:

    <beans>
    <bean id=”cpAction ” class=”cn.ricki.cheung.action.CpAction”/>
    </beans>

    修改strust.xml:

    <action name=”CpAction”  class=”cpAction >
    <result type=”json”/>
    </action>

    注意上面黑体显示的部分,在struts.xml中class属性的值为Spring配置文件中某bean id的值,它告诉 Struts2向Spring获取id为cpAction的bean。

    其实在上面提到的struts.xml中不只一个Action,除了CpAction外,还有很多,但那些我并没有修改,而程序依然可以运行,原因看下面。
    假如struts.xml内容如下:

    <action name=”LabelAction” class=”cn.ricki.cheung.action.LabelAction”>
    <result type=”json”/>
    </action>
    <action name=”CpAction”  class=”cpAction >
    <result type=”json”/>
    </action>

    当客户端发送请求并由LabelAction处理时,Struts首先向Spring索取id为 cn.ricki.cheung.action.LabelAction的bean,但Spring配置文件中并没有这个id的bean,这 时,Spring试着创建cn.ricki.cheung.action.LabelActio对象,并返回给Struts。

  • 相关阅读:
    C# 生成windows 服务打包程序
    Ext.Net 复制GridPanel的数据
    The web.config file for this project is missing the required DirectRequestModule.
    微软 Remote App
    IIS 中的虚拟目录 和软连接
    C# 生成word 文档 代码 外加 IIS报错解决方案
    .NET:序列化和反序列化
    设计模式:常见设计模式适用的场景
    WebApi:自定义筛选器
    log4net:保存自定义参数到数据库
  • 原文地址:https://www.cnblogs.com/zqyanywn/p/7139865.html
Copyright © 2011-2022 走看看