您的位置:首页 > 编程语言 > Java开发

细谈Spring(十一)深入理解spring+struts2整合(附源码)

2016-09-16 00:00 615 查看
摘要: spring+structs

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

我们一起来想想,如果让spring和struts2进行整合,我们就希望我们可以在spring中直接注入action,spring容器初始化的时候就给我们建好了action,但是事情不像我们想象的那么简单,因为struts2的action是由struts2自己new出来的,他不受spring的管理,所以无法自动注入。所以struts和spring的整合的结合点在于,struts2的action不能直接入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,一旦我们的项目启动就触发了监听器,我们来看一下这个监听器的配置:

[html] view plain copy print?
<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

<!-- default: /WEB-INF/applicationContext.xml -->

</listener>

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

[html] view plain copy print?
<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--plugin。Xml文件。下面我们来看一下这个文件中的配置执行的具体过程:

[html] view plain copy print?
<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一起动就会去加载配置文件,其中包括struts—plugin。xml读取顺序:
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容器,这个容器是struts—plugin提供的,他会把所有的action加载都加载到自己的容器里。然后根据action的属性名字自动去spring去找名字和action属性相同的bean直接注入到action中,也就是说。我们在action中其实不用配置注入的东西,struts容器会自动给我们注入。但还是要提供相应的set、get方法。并且名字要约定好,action属性和spring中的bean的id要一样。但不要忘记,action的scope设置成prototype

下面我们来看一下具体的示例代码:
[java] view plain copy print?
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"));

//

//}

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

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

[html] view plain copy print?
<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
[html] view plain copy print?
<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的容器来产生action,spring需要的时候要去struts容器里面去找action,然后再初始化bean,其实我们还有一种方法产生action,那就是让spring容器去帮我们来产生action,这样我们产生action的时候就可以去spring容器里面去找了,具体应该是在spring配置文件beans.xml中把对应的action配置成bean,然后再struts.xml中配置action的时候,action对应的class就不再是配置成该action对应的类了,而是配置这个action对应的spring容器的bean的id属性,这样action初始化的时候就会去spring容器里面去找了。但是这样配置的话,我们的action属性就必须配置了,因为spring来产生action后,struts容器就不会在自动去给我们注入属性了。如果不配置属性的话会产生异常,下面我们来看一下具体配置情况:

Action的代码还是以前的代码,没有改变,这里就不再重复写了,下面我们来看一下struts.xml的配置:
Struts.xml
[html] view plain copy print?
<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对应的是下面action中bean的id属性
Beans.xml

[html] view plain copy print?

<pre name="code" class="html"><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="stuAction" class="com.bzu.action.StudentAction" scope="prototype">
<property name="service" ref="service"></property>
</bean></pre><br>

<br>
<pre></pre>
<p></p>
<pre></pre>
<p></p>
<p></p>
<p></p>
<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>
<p></p>
<pre></pre>
<pre></pre>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring structs java
相关文章推荐