`
dreamoftch
  • 浏览: 485290 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring和struts2集成的一些认识

阅读更多

 

 注:通常我们的action都要继承ActionSupport,这里都默认继承了该类

 

参考资料:

 

http://struts.apache.org/release/2.2.x/docs/spring-plugin.html

 

http://struts.apache.org/release/2.2.x/docs/spring-and-struts-2.html

 

要想将struts2和spring整合起来,只需要引入 struts2-spring-plugin-x-x-x.jar 即可

 

然后打开jar包,发现里面有一个struts-plugin.xml的文件:

 

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
    
<struts>

    
    <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />

    <!-- 这里指定了struts2中的对象生成的方式交给spring来管理-->
    <constant name="struts.objectFactory" value="spring" />

    <constant name="struts.class.reloading.watchList" value="" />
    <constant name="struts.class.reloading.acceptClasses" value="" />
    <constant name="struts.class.reloading.reloadConfig" value="false" />

    <package name="spring-default">
        <interceptors>
            <!--这里定义了给action装配属性的拦截器,作用是给那些由struts2生成的action装配他们所依赖的属性-->
            <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
        </interceptors>
    </package>    
</struts>

 

 

这个plugin 的三个功能:

 

  • Allow Actions, Interceptors, and Results to be created by Spring
  • Struts-created objects can be autowired by Spring after creation
  • Provides two interceptors that autowire actions, if not using the Spring ObjectFactory

 

第一:允许spring来控制Actions, Interceptors, and Results 的创建  

 

 

第二: 那些由struts2 自身创建的对象在创建之后,他们所依赖的属性由spring来装配

 

第三:对于那些struts2自身创建的action,spring提供了两个拦截器来为他们装配属性

 

 

 

 下面通过例子来详细的理解一下:

 

struts.xml 的内容为:

 

 

<action name="edit" class="com.tch.test.temp.test.EditAction" method="input">
	<result name="input">/edit.jsp</result>
</action>

 

 

 

 

 

 

假如我们的action里面有这样一个方法 setEditService(EditService editService)

 

 

package com.tch.test.temp.test;

import com.opensymphony.xwork2.ActionSupport;

public class EditAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	private EditService editService;

	public void setEditService(EditService editService) {
		this.editService = editService;
	}

        public String input(){
		return SUCCESS;
	}
}

 

 

 其中spring容器管理着  EditServiceImpl 对象(注意,这里的action对象没有被spring容器管理):

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="editService" class="org.apache.struts.edit.service.EditServiceImpl" />

</beans>

 

 

 

这种情况下,我们的action仍旧是被struts2 容器所创建的,但是它所依赖的 editService 属性,则是由spring来生成并负责注入的。

参考 http://struts.apache.org/release/2.2.x/docs/spring-and-struts-2.html  里面的:

(Using the above methodology, the Struts 2 framework will still manage the creation of the ActionSupport class. If you prefer you can configure the application so that Spring will create the ActionSupport class also. To support this technique you need to add a bean node to the Spring configuration file for the ActionSupport class.)

 

 

为什么是struts容器负责创建我们的action呢,这是因为action创建的时候,首先会去查看spring配置文件里面是否有id属性和struts配置文件中的class属性一致的对象,

如果有,则说明该对象已经由spring容器创建,struts就不会再自己创建了,如果发现spring容器没有创建,那么,struts容器就会自己创建该action,所以,

上面就是首先查看spring里面有没有id属性等于  struts.xml里面的class属性的对象(com.tch.test.temp.test.EditAction),最后发现没有,所以struts就自己创建该对象啦

 

 

好的,下面我们修改一下:

 

我们把spring配置文件改成这个样子:

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
            

<bean id="editService" class="org.apache.struts.edit.service.EditServiceImpl" />

<bean id="editAction" class="org.apache.struts.edit.action.EditAction" >

	<property name="editService" ref="editService" />
	
</bean>

</beans>

 

 

 

 把struts.xml 文件改成这样:

 

<action name="edit" class="editAction" method="input">
	<result name="input">/edit.jsp</result>
</action>

 

 

 

注意,struts.xml 这里的class属性是editAction, spring配置文件中也存在这样一个id是editAction的对象,并且类型一样

 

这个时候,struts会发现spring以及创建了这样的一个action,自己就不会在创建了,这样就实现了把action也交给spring来管理的目的。

 

当然,这两张做法都是可以的,没有那个是最好的,实际工作中也都有使用

 

 

 上面的内容是在阅读了http://struts.apache.org/release/2.2.x/docs/spring-and-struts-2.html  之后的一点收获,睡觉前记录一下

 

也算是解决了之前自己的一点小疑惑,之前工作使用的是struts创建action,没有交给spring管理,所以以前一直奇怪,为什么struts创建的action可以被spring来注入它所依赖的属性。

 

现在明白了,是因为这个plugin里面提供了一个专门负责做这个装配工作的拦截器

 

 

最后需要注意的是如果将action交给spring管理,要将action设为非单例的,在xml中通过<bean id="bar" class="com.my.BarClass" singleton="false"/>来设置,

 

如果用注解的话,通过@Scope("prototype")来设置即可

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics