1、在spring中注入action
缺点在于:
在struts.xml中写
<action name="loginAction" class="loginAction" />
同时在spring配置文件中需要写
<bean id="loginAction" class="com.lk.loginAction" >
<property ref="service" />
</bean>
这样看来配置文件要比第一种要繁琐一些。
但是据称这种方式。适合配置AOP的内容。而且struts2官方文档上也是采用的这种配置
2、采用自动装配方式,即不在spring中注入action(系统默认的);
业务类在Spring配置文件中配置,Action不需要配置,Struts2的Action像没有整合Spring之前一样配置,<action>的class属性指定业务逻辑控制器类的全限定名。
Action中引用的业务类不需要自己去初始化,Struts2的Spring插件会使用bean的自动装配将业务类注入进来,其实Action也不是Struts2创建的,而是Struts2的Spring插件创建的。默认情况下,插件使用by name的方式装配,可以通过增加Struts2常量来修改匹配方式:设置方式为:
<constant name="struts.objectFactory.spring.autoWire" value="name" />
可选的装配参数如下:
name:相当于spring配置的autowrie="byName"(默认)
type:相当于spring配置的autowrie="byType"
auto:相当于spring 配置的autowrie="autodetect"
constructor: 相当于spring配置的autowrie="constructor"
好处在于:不必在struts.xml中写了配置文件后,又在spring的配置文件中再写一遍配置
如:
在struts.xml中写
<action name="loginAction" class="com.lk.loginAction" />
就可以了。
额外需要两个jar包:struts2-spring-plugin-2.3.15.1.jar 和 commons-lang3-3.1.jar 缺一不可