整合strust2和spring的关键点(为action注入service)

 Springstruts2是我们在项目架构中用的比较多的两个框架,怎么才能把这两个框架用好,怎么来整合是我们掌握运用这两个框架的关键点,下面我们就怎么来整合,从哪来整合,为什么要整合,从这几点来看一下struts2spring的整合。下面我们来具体分析一下:


我们一起来想想,如果让springstruts2进行整合,我们就希望我们可以在spring中直接注入actionspring容器初始化的时候就给我们建好了action,但是事情不像我们想象的那么简单,因为struts2action是由struts2自己new出来的,他不受spring的管理,所以无法自动注入。所以strutsspring的整合的结合点在于,struts2action不能直接入service。好了,既然有了问题,spring或者struts2肯定已经为我们把这个问题解决了。struts2解决这个问题是他给我们提供了一个struts-spring-plugin的插件,通过这个插件我们就可以把我们的struts2和是spring进行整合了。struts-spring-plugin将会对我们的action进行管理,当spring需要action的时候他就可以向struts-spring-plugin来要了。


源码下载:用力点


下面我们就具体的来看一下struts+spring的整合过程:


1.需要的jar包列表:Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2


jar包名称

所在位置

说明

antlr-2.7.6.jar

hibernate/lib/required

解析HQL

aspectjrt

spring/lib/aspectj

AOP

aspectjweaver

..

AOP

cglib-nodep-2.1_3.jar

spring/lib/cglib

代理,二进制增强

common-annotations.jar

spring/lib/j2ee

@Resource

commons-collections-3.1.jar

hibernate/lib/required

集合框架

commons-fileupload-1.2.1.jar

struts/lib

struts

commons-io-1.3.2

struts/lib

struts

commons-logging-1.1.1

单独下载,删除1.0.4(struts/lib)

struts

spring

dom4j-1.6.1.jar

hibernate/required

解析xml

ejb3-persistence

hibernate-annotation/lib

@Entity

freemarker-2.3.13

struts/lib

struts

hibernate3.jar

hibernate

hibernate-annotations

hibernate-annotation/

hibernate-common-annotations

hibernate-annotation/lib

javassist-3.9.0.GA.jar

hiberante/lib/required

hibernate

jta-1.1.jar

..

hibernate transaction

junit4.5

mysql-

ognl-2.6.11.jar

struts/lib

slf4j-api-1.5.8.jar

hibernate/lib/required

hibernate-log

slf4j-nop-1.5.8.jar

hibernate/lib/required

spring.jar

spring/dist

struts2-core-2.1.6.jar

struts/lib

xwork-2.1.2.jar

struts/lib

struts2

commons-dbcp

spring/lib/jarkata-commons

commons-pool.jar

..

struts2-spring-plugin-2.1.6.jar

struts/lib



2.配置好jar包以后,如果我们想在服务器一启动就可以让spring容器自动去加载我们在配置文件中配置的bean,那么我们要在web.xml中去配置一个监听器,这个监听器的作用是监听我们的application,一旦我们的项目启动就触发了监听器,我们来看一下这个监听器的配置:


  1. <listener>
  2. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. <!-- default: /WEB-INF/applicationContext.xml -->
  4. </listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- default: /WEB-INF/applicationContext.xml -->
</listener>


如果你的配置文件不想放在默认的位置,而是自己去指定位置,那么我们将要在web.xml中再次配置如下:


  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. //这种配置可以指定多个配置文件,因为spring的配置文件可以分开写成好几个
  4. <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> -->
  5. //指定spring配置文件的位置classpath下的beans.xml
  6. <param-value>classpath:beans.xml</param-value>
  7. </context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
//这种配置可以指定多个配置文件,因为spring的配置文件可以分开写成好几个
<!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->
//指定spring配置文件的位置classpath下的beans.xml
<param-value>classpath:beans.xml</param-value>
</context-param>


加载完上面的bean之后,我们就要考虑去管理我们的action了,我们应该让spring去找struts去要相应的action,把action实例化为响应的bean,这时我们就需要我们上边所提到的struts-spring-plugin这个jar包了。加上这个jar包之后我们就可以让spring来管理我们的action了。在struts-spring-plugin中有一个struts--pluginXml文件。下面我们来看一下这个文件中的配置执行的具体过程:


  1. <struts>
  2. <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
  3. <!-- Make the Spring object factory the automatic default -->
  4. <constant name="struts.objectFactory" value="spring" />
  5. <package name="spring-default">
  6. <interceptors>
  7. <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
  8. <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
  9. </interceptors>
  10. </package>
  11. </struts>
<struts>
    <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
    
    <!--  Make the Spring object factory the automatic default -->
    <constant name="struts.objectFactory" value="spring" />
    <package name="spring-default">
        <interceptors>
            <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
            <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
        </interceptors>
    </package>    
</struts>


关键是这个 <constant name="struts.objectFactory" value="spring" />这句配置就指明了我们产生struts对象的工厂交给spring来产生我们来看一下具体步骤:


struts2一起动就会去加载配置文件,其中包括strutspluginxml读取顺序:

struts的读常量:

struts-default.xml

struts-plugin.xml

struts.xml

struts.properties

web.xml


struts-plugin.xml指明了我们产生对象的工厂交给spring来完成,当执行到web.xml时,由于spring容器的监听器,这时spring容器就开始启动,spring启动之后会web.xml去找相应的配置,在web.xml中可以找到spring中的配置文件beans.xml,然后去初始化所有的bean

spring去加载beans.xml的时候会自动把所有的bean初始化,并且放在自己的容器里。与此同时,struts2也有自己的bean容器,这个容器是strutsplugin提供的,他会把所有的action加载都加载到自己的容器里然后根据action的属性名字自动去spring去找名字和action属性相同的bean直接注入到action,也就是说。我们在action中其实不用配置注入的东西,struts容器会自动给我们注入。但还是要提供相应的setget方法。并且名字要约定好,action属性和spring中的beanid要一样。但不要忘记,actionscope设置成prototype


下面我们来看一下具体的示例代码:

  1. package com.bzu.action;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import com.bzu.entity.Student;
  4. import com.bzu.service.StudentService;
  5. import com.opensymphony.xwork2.ActionSupport;
  6. public class StudentAction extends ActionSupport {
  7. private Student student;
  8. private StudentService service;
  9. @Override
  10. public String execute() throws Exception {
  11. // TODO Auto-generated method stub
  12. service.login(student);
  13. return SUCCESS;
  14. }
  15. public StudentService getService() {
  16. return service;
  17. }
  18. public Student getStudent() {
  19. return student;
  20. }
  21. public void setStudent(Student student) {
  22. this.student = student;
  23. }
  24. public void setService(StudentService service) {
  25. this.service = service;
  26. }
  27. //public static void main(String[] args) {
  28. //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  29. //"beans.xml");
  30. //
  31. //StudentAction action = (StudentAction) context.getBean("StudentAction");
  32. //
  33. //System.out.println(action == null);
  34. //
  35. //StudentService ser = action.getService();
  36. //
  37. //ser.login(new Student("ss", "ss"));
  38. //
  39. //}
package com.bzu.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bzu.entity.Student;
import com.bzu.service.StudentService;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport {
private Student student;
private StudentService service;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
service.login(student);
return SUCCESS;
}
public StudentService getService() {
return service;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public void setService(StudentService service) {
this.service = service;
}
//public static void main(String[] args) {
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
//"beans.xml");
//
//StudentAction action = (StudentAction) context.getBean("StudentAction");
//
//System.out.println(action == null);
//
//StudentService ser = action.getService();
//
//ser.login(new Student("ss", "ss"));
//
//}


需要注意的是,springstruts2整合的时候有一个属性struts.objectFactory.spring.autoware,也就是说是struts属性的的自动装配类型,他的默认值是name,也就是说struts中的action中的属性不需要配置,他默认的去beans.xml中去找名字相同的,应该注意的是,在给一些属性起名字的时候不要和spring中配置的actionname属性相同,否则会报异常


下面我们来看一下struts.xmlbeans.xml的相关配置:

Struts.xml:


  1. <struts>
  2. <package name="registration" extends="struts-default">
  3. <action name="StudentAction" class="com.bzu.action.StudentAction">
  4. <result name="success">/Success.jsp</result>
  5. <result name="fail">/Fail.jsp</result>
  6. </action>
  7. </package>
  8. </struts>
<struts>
<package name="registration" extends="struts-default">
<action name="StudentAction" class="com.bzu.action.StudentAction">
<result name="success">/Success.jsp</result>
<result name="fail">/Fail.jsp</result>
</action>
</package>
</struts>


Beans.xml

  1. <bean id="stuDao" class="com.bzu.dao.impl.StudentDaoImpl"></bean>
  2. <bean id="service" class="com.bzu.service.impl.StudentServiceImpl">
  3. <property name="dao" ref="stuDao"></property>
  4. </bean>
  5. <!--这里完全可以不写-->
  6. <!--
  7. <bean id="StudentAction" class="com.bzu.action.StudentAction" scope="prototype">
  8. <property name="service" ref="service"></property>
  9. </bean>
  10. -->
<bean id="stuDao" class="com.bzu.dao.impl.StudentDaoImpl"></bean>
<bean id="service" class="com.bzu.service.impl.StudentServiceImpl">
<property name="dao" ref="stuDao"></property>
</bean>
<!--这里完全可以不写-->
<!--
<bean id="StudentAction" class="com.bzu.action.StudentAction" scope="prototype">
<property name="service" ref="service"></property>
</bean>
-->


上面的示例是用的struts2的容器来产生actionspring需要的时候要去struts容器里面去找action,然后再初始化bean,其实我们还有一种方法产生action,那就是让spring容器去帮我们来产生action,这样我们产生action的时候就可以去spring容器里面去找了,具体应该是在spring配置文件beans.xml中把对应的action配置成bean,然后再struts.xml中配置action的时候,action对应的class就不再是配置成该action对应的类了,而是配置这个action对应的spring容器的beanid属性,这样action初始化的时候就会去spring容器里面去找了。但是这样配置的话,我们的action属性就必须配置了,因为spring来产生action后,struts容器就不会在自动去给我们注入属性了。如果不配置属性的话会产生异常,下面我们来看一下具体配置情况:

Action的代码还是以前的代码,没有改变,这里就不再重复写了,下面我们来看一下struts.xml的配置:

Struts.xml

  1. <struts>
  2. <package name="registration" extends="struts-default">
  3. <action name="StudentAction" class="stuAction">
  4. <result name="success">/Success.jsp</result>
  5. <result name="fail">/Fail.jsp</result>
  6. </action>
  7. </package>
  8. </struts>
<struts>
<package name="registration" extends="struts-default">
<action name="StudentAction"  class="stuAction">
<result name="success">/Success.jsp</result>
<result name="fail">/Fail.jsp</result>
</action>
</package>
</struts>


上面的class对应的是下面actionbeanid属性

Beans.xml

  1. <PRE class=html name="code"><bean id="stuDao" class="com.bzu.dao.impl.StudentDaoImpl"></bean>
  2. <bean id="service" class="com.bzu.service.impl.StudentServiceImpl">
  3. <property name="dao" ref="stuDao"></property>
  4. </bean>
  5. <!--现在这里必须写了,不写会报错-->
  6. <bean id="stuAction" class="com.bzu.action.StudentAction" scope="prototype">
  7. <property name="service" ref="service"></property>
  8. </bean></PRE><BR>
  9. <BR>
  10. <PRE></PRE>
  11. <P></P>
  12. <PRE></PRE>
  13. <P></P>
  14. <P></P>
  15. <P></P>
  16. <P><SPAN style="FONT-SIZE: 18px"> OK<SPAN style="FONT-FAMILY: 宋体"></SPAN><SPAN style="FONT-FAMILY: Courier New">struts2+spring</SPAN><SPAN style="FONT-FAMILY: 宋体">整合讲到这就基本完了,当然我说的也不是很全面,希望大家见谅,希望大家能提出宝贵意见</SPAN></SPAN></P>
  17. <P></P>
  18. <PRE></PRE>
  19. <PRE></PRE>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章