码迷,mamicode.com
首页 > 编程语言 > 详细

[SSH]struts2-spring-plugin.jar了解

时间:2017-07-09 10:49:44      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:cat   jar   请求   frame   rri   设置   ssh   cto   obj   

  在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。

[SSH]struts2-spring-plugin.jar了解

标签:cat   jar   请求   frame   rri   设置   ssh   cto   obj   

原文地址:http://www.cnblogs.com/zqyanywn/p/7139865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!