一、
需要的
JAR
文件为:
Spring
和
Struts2
框架本身需要的
JAR
文件以及他们所依赖的
JAR文件,比如commons-logging.jar
等等,另外还需要
Struts2发布包中的struts2-spring-plugin-x.xx.jar。
二、整合过程:
分两步:配置web.xml+整合struts.xml及applicationContext.xml.
(1)
在
web.xml
中增加
WebApplicationContext
的相应配置,
以下两种配置方式本质是一样的。
1.
Servlet 2.3
及以上版本可以使用监听器,相应配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果
spring
配置文件被命名为
applicationContext.xml
,并且放在
WEB-INF
目录下,则不需要配置
<context-param>
,因为
ContextLoaderListener
默认在
WEB-INF
目录下寻找名为
applicationContext.xml
的文件。
若要放于WEB-INF/classes/目录下,或若存在多个
Spring
配置文件,则需要上面的配置,且在
<param-value>
中依次列出,之间以逗号隔开。
2.
Servlet 2.3
以下版本由于不支持
<listener>
,需要配置
<servlet>
,格式如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>contextLoaderServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
由于该
Servlet
配置只是为了在容器启动时能启动
ContextLoaderServlet
使其工作,而不需要引用该
Servlet
,所以不需要配置
<servlet-mapping>
。
(2)
在web.xml中完成加载WebApplicationContext之后,接下来就可以做到Spring和Struts2
的整合了:就是将业务类具体映射放到spring中。
整合有两种方法,分别叙述如下:
1. 第一种实现方法:
1) 在struts.xml中,指定<action>的class属性为Spring配置文件中相应bean的id或者name值。示例如下:
<action name=”LoginAction” class=”LoginAction”>
<result name=”success”>/index.jsp</result>
</action>
2)
将
Struts
的业务逻辑控制器类映射声明定义在
applicationContext.xml
文件中。注意,必须将业务逻辑控制器类配置为
scope=”prototype”
!
示例如下:
<bean id=”LoginAction” class=”yaso.struts.action.LoginAction”>
<property name=”loginDao” ref=”LoginDao”/>
</bean>
与第一种实现方法相同,只是第一种方式,是手动注入依赖对象,这里是采用autoWire来自动注入依赖对象。
如下:
struts.xml中(同上):
<action name=”LoginAction” class=”LoginAction”>
<result name=”success”>/index.jsp</result>
</action>
applicationContext.xml中:
<bean id=”LoginAction” class=”yaso.struts.action.LoginAction” scope="prototype" autoWire="byName">
默认情况下,插件使用
by name
的方式装配,可以通过增加
Struts2
常量来修改匹配方式:设置方式为:
struts.objectFactory.spring.autoWire = typeName
,可选的装配参数如下:
a)
name
:等价于
Spring
配置中的
autowire=”byName”
,这是缺值。
b)
type
:等价于
Spring
配置中的
autowire=”byType”
。
c)
auto
:等价于
Spring
配置中的
autowire=”autodetect”
。
d)
constructor
:等价于
Spring
配置中的
autowire=” constructor”
。
四、
如果原先在
Struts2
中使用了多个
object factory
,则需要通过
Struts2
常量显式指定
object factory
,方式如下:
struts.objectFactory = spring
;如果没有使用多个
object factory
,这一步可以省略。
五、
可以通过设增加
Struts2
常量来指定是否使用
Spring
自身的类缓存机制。可以设定的值为
true
或
false
,默认为
true
。设置方式为:
struts.objectFactory.spring.useClassCache = false
。
六、
至此,完成了两种方式的整合。比较这两种整合方式,其本质是一样的。不同之处在于,使用第二种自动装配的方式时,由于没有在
Spring中配置业务逻辑控制器,所以需要对其配置一些AOP之类的内容时就很难实现了。
Spring Autowire自动装配
在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象。但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的<ref>标签。
Spring为我们提供了一个自动装配的机制,在定义Bean时,<bean>标签有一个autowire属性,我们可以通过指定它来让容器为受管 JavaBean自动注入依赖对象。
<bean>的autowire属性有如下六个取值,他们的说明如下:
1、 No:即不启用自动装配。Autowire默认的值。
2、 byName:通过属性的名字的方式查找JavaBean依赖的对象并为其注入。比如说类Computer有个属性printer,指定其 autowire属性为byName后,Spring IoC容器会在配置文件中查找id/name属性为printer的bean,然后使用Seter方法为其注入。
3、 byType:通过属性的类型查找JavaBean依赖的对象并为其注入。比如类Computer有个属性printer,类型为Printer,那么,指定其autowire属性为byType后,Spring IoC容器会查找Class属性为Printer的bean,使用Seter方法为其注入。
4、 constructor:通byType一样,也是通过类型查找依赖对象。与byType的区别在于它不是使用Seter方法注入,而是使用构造子注入。
5、 autodetect:在byType和constructor之间自动的选择注入方式。
6、 default:由上级标签<beans>的default-autowire属性确定。
注意:在配置bean时,<bean>标签中Autowire属性的优先级比其上级标签高,即是说,如果在上级标签中定义default- autowire属性为byName,而在<bean>中定义为byType时,Spring IoC容器会优先使用<bean>标签的配置。
下面通过一个例子来说明如何在应用中使用自动装配(工程代码见例程3.2)。新建一个java工程,为其添加上Spring开发能力后,创建一个ioc.test包,再分别创建电脑类(Computer)、主机类(Host)和显示器类(Dispaly),为电脑类添加Host类型的属性host和Display类型的属性display,再添加一个run方法,让电脑可以“运行”一起来。属性代码如下:
1package ioc.test;
2
3 /** *//**
4 * @author zhangyong
5*/
6public class Computer {
7
8 private Host host;
9 private Display display;
10 //电脑运行方法
11 public void run(){
12 System.out.println( "你好,我是电脑,正在运行!");
13 System.out.print( " "+host.run()+ ",");
14 System.out.println(display.run());
15 }
16 //Geter和Seter方法,省略
17}
18
1package ioc.test;
2
3public class Host {
4 public String run(){
5 return "我是主机,正在运行!";
6 }
7}
8
1package ioc.test;
2
3public class Display {
4 public String run(){
5 return "我是显示器,正在运行!";
6 }
7}
8
下面便是修改Spring的配置文件,让IoC容器为我们的“电脑”自动装配“主机”和“显示器了”。分别配置两个bean,host和display。再配置一个名computer1的bean,autowire属性设为byName,同理配置computer1和computer3,autowire属性分别设为byType和default,最后设置<beans>标签的default-autowire属性为autodetect。至此,配置工作已经完成 ,可以看到,我们并没有显式的给computer bean它注入依赖对象host和display。配置代码如下:
1<?xml version= "1.0" encoding= "UTF-8"?>
2<beans default-autowire= "autodetect">
3 <bean id= "computer1" class= "ioc.test.Computer" autowire= "byName"></bean>
4 <bean id= "computer2" class= "ioc.test.Computer" autowire= "byType"></bean>
5 <bean id= "computer3" class= "ioc.test.Computer" autowire= "default"></bean>
6
7 <bean id= "host" class= "ioc.test.Host"></bean>
8 <bean id= "display" class= "ioc.test.Display"></bean>
9</beans>
10
现在可以建立一个测试类来测试一下Spring时候真的为我们自动装配好了我们需要的bean。
1package ioc.test;
2
3 //import省略
4public class TestMain {
5
6 public static void main(String[] args) {
7 ApplicationContext ac = new ClassPathXmlApplicationContext(
8 "applicationContext.xml");
9 //byName
10 Computer computer1 = (Computer)ac.getBean( "computer1");
11 System.out.println( "autowire=\"byName\":");
12 computer1.run();
13
14 //byType
15 Computer computer2 = (Computer)ac.getBean( "computer2");
16 System.out.println( "autowire=\"byType\":");
17 computer2.run();
18
19 //default
20 Computer computer3 = (Computer)ac.getBean( "computer3");
21 System.out.println( "autowire=\"default\":");
22 computer3.run();
23 }
24}
25