在Struts2中整合Spring的IoC支持是一件十分简单的事情。让我们一步一步来实现:
1)复制class="tags" href="/tags/STRUTS.html" title=struts>struts2-class="tags" href="/tags/SPRING.html" title=spring>spring-plugin-x-x-x.jar和相应的class="tags" href="/tags/SPRING.html" title=spring>spring.jar到/WEB-INF/lib目录下。
2)在class="tags" href="/tags/STRUTS.html" title=struts>struts.properties中设置class="tags" href="/tags/STRUTS.html" title=struts>struts.objectFactory属性值
或者是在XML文件中进行常量配置
3)配置Spring监听器
通过Spring配置来注册对象
<?xml version=class="tags" href="/tags/ENCODING.html" title=encoding>encoding="UTF-8"?>
<!DOCTYPE class="tags" href="/tags/BEAN.html" title=bean>beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.class="tags" href="/tags/SPRING.html" title=spring>springframework.org/dtd/class="tags" href="/tags/SPRING.html" title=spring>spring-class="tags" href="/tags/BEAN.html" title=bean>beans.dtd">
<class="tags" href="/tags/BEAN.html" title=bean>beans default-autowire="byName">
<class="tags" href="/tags/BEAN.html" title=bean>bean id="personManager" class="com.acme.PersonManager"/>
</class="tags" href="/tags/BEAN.html" title=bean>beans>
当然你也可以拥有更多的Spring配置文件。在web.xml中进行下列设置,从而使Spring的ApplicationContext通过匹配所给定模式的文件来初始化对象
<!-- 用来定位Spring XML文件的上下文配置 -->
<context-param>
<param-name>contextConfigLocation
4)修改你的Struts配置文件
<!DOCTYPE class="tags" href="/tags/STRUTS.html" title=struts>struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://class="tags" href="/tags/STRUTS.html" title=struts>struts.apache.org/dtds/class="tags" href="/tags/STRUTS.html" title=struts>struts-2.0.dtd">
<class="tags" href="/tags/STRUTS.html" title=struts>struts>
<include file="class="tags" href="/tags/STRUTS.html" title=struts>struts-default.xml"/>
<package name="default" extends="class="tags" href="/tags/STRUTS.html" title=struts>struts-default">
<class="tags" href="/tags/ACTION.html" title=action>action name="foo" class="com.acme.Foo">
<result>foo.ftl</result>
</class="tags" href="/tags/ACTION.html" title=action>action>
</package>
<package name="secure" namespace="/secure" extends="default">
<class="tags" href="/tags/ACTION.html" title=action>action name="bar" class="bar">
<result>bar.ftl</result>
</class="tags" href="/tags/ACTION.html" title=action>action>
</package>
</class="tags" href="/tags/STRUTS.html" title=struts>struts>
默认情况下,Spring从下面的文件中寻找为bar所做的配置
<?xml version="1.0" class="tags" href="/tags/ENCODING.html" title=encoding>encoding="UTF-8"?>
<!DOCTYPE class="tags" href="/tags/BEAN.html" title=bean>beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.class="tags" href="/tags/SPRING.html" title=spring>springframework.org/dtd/class="tags" href="/tags/SPRING.html" title=spring>spring-class="tags" href="/tags/BEAN.html" title=bean>beans.dtd">
<class="tags" href="/tags/BEAN.html" title=bean>beans default-autowire="byName">
<class="tags" href="/tags/BEAN.html" title=bean>bean id="bar" class="com.my.BarClass" singleton="false"/>
...
</class="tags" href="/tags/BEAN.html" title=bean>beans>
5)好了,现在你的Struts2和Spring就能正常的一起工作了。有几个配置技术点需要详细说明下:装配模式。你可以通过设置修改class="tags" href="/tags/STRUTS.html" title=struts>struts.properties中下列属性的值来改变装配模式。
该参数可供选择的设置值有
name | 按照你的class="tags" href="/tags/ACTION.html" title=action>action的属性的名字和Spring里的class="tags" href="/tags/BEAN.html" title=bean>bean的名字匹配,如果匹配就自动装配。这是缺省的 |
---|---|
type | 按照你的class="tags" href="/tags/ACTION.html" title=action>action的属性的类型,在Spring注册的class="tags" href="/tags/BEAN.html" title=bean>bean中查找,如果相同就自动装配。这需要你在Spring中仅注册了一个此类型的class="tags" href="/tags/BEAN.html" title=bean>bean |
auto | Spring会试图自动监测来找到最好的方法自动装配你的class="tags" href="/tags/ACTION.html" title=action>action |
constructor | Spring会自动装配class="tags" href="/tags/BEAN.html" title=bean>bean的构造函数的参数 |
是否使用类缓存。你可以通过设置修改class="tags" href="/tags/STRUTS.html" title=struts>struts.properties中下列属性的值来改变是否使用Spring自身的类缓存机制。可以设定的值为true或false,默认为true。