SSH三大框架整合配置详细步骤(3)

5 配置Spring2.5

 

5.1 基础配置

1)        导入spring包。下载spring-framework-2.5.6并解压后,在spring-framework-2.5.6"dist目录下找到spring.jar,引入到工程中。java

说明:spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring-mock.jar里所包含的内容外其它全部jar包的内容,由于只有在开发环境下才会用到spring-mock.jar来进行辅助测试,正式应用系统中是用不得这些类的。除了spring.jar文件,Spring还包括有其它13个独立的jar包,各自包含着对应的Spring组件,用户能够根据本身的须要来选择组合本身的jar包,而没必要引入整个spring.jar的全部类文件。这里,为了使用方便,咱们引入整个spring.jar。web

2)        配置web.xml文件。Jar包引入完成后,就开始配置spring了,首先修改web.xml文件,增长以下代码:spring

<!-- 
从类路径下加载spring的配置文件, 多个配置文件能够用逗号和空格区分
classpath: 关键字特指类路径下加载-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:applicationContext*.xml
        </param-value>
    </context-param>
express

在这里,咱们指定了spring配置文件的路径,即WEB-INF/classes/spring目录下的全部以applicationContext开头命名的xml文件。session

3)        在src下面新建applicationContext.xml文件。首先给这个文件加上spring的标头:app

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

注意:标头是2.5的 不要引入2.0, 错了可能Spring就不能正确加载。 测试

5.2 示例

       Spring基本配置完毕,让咱们建个示例来测试一下吧,首先在test.spring包下建立两个java文件:TUser.java、SpringTest.java。this

TUser.java:spa


 1package test.spring;
 2
 3public class TUser implements java.io.Serializable {
    private String username;
    private String allname;
    private String address;
 7
    public String getUsername() {
        return this.username;
10    }
11    public void setUsername(String username) {
12        this.username username;
13    }
14    public String getAllname() {
15        return this.allname;
16    }
17    public void setAllname(String allname) {
18        this.allname allname;
19    }
20    public String getAddress() {
21        return this.address;
22    }
23    public void setAddress(String address) {
24        this.address address;
25    }
26}
27
 

SpringTest.java:hibernate


 1package test.spring;
 2
 3import org.springframework.context.ApplicationContext;
 4import org.springframework.context.support.ClassPathXmlApplicationContext;
 5
 6public class SpringTest {
    public static void main( String[] args {
        //加载spring配置文件,初始化IoC容器
        ApplicationContext ac new ClassPathXmlApplicationContext("applicationContext.xml");
10        //从容器 接管Bean
11        TUser user (TUser) ac.getBean("TUser");
12        //输出欢迎信息
13        System.out.println( "Hello:" user.getUsername() ";u is in user.getAddress() and is  user.getAllname() );
14    }
15}
16
 

建立完毕后,就剩最后一步了,在applicationContext.xml中配置一个bean,在xml中增长以下代码:

<bean id="TUser" class="test.spring.TUser">
        <property name="username" value="小张"></property>
        <property name="allname" value="张三"></property>
        <property name="address" value="青岛市"></property>
    </bean> 

好了,下面运行一下吧,右键单击SpringTest.java选择run as àJava Application,运行结果以下:

输出 Hello:小张;u is in 青岛市; and u is 张三

若是你的运行结果和上面同样,且没有异常,则说明Spring配置成功了。是否是很简单?不要骄傲,重要的是Spring与Hibernate、Struts的整合。继续吧! 

5.3 整合Struts

       Spring与Struts的整合其实就是把Struts的Action类交给Spring来管理,下面开始吧!

1)        导入jar包。在Struts2.1.6的lib目录中找到struts2-spring-plugin-2.1.6.jar,引入到工程中。

2)        配置web.xml文件。在web.xml中加入如下代码:

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

1)        如今就来看如何把struts的action交给spring。以struts示例中的login.action为例,首先建立一个LoginAction类的Bean。在applicationContext.xml中增长以下代码:

<bean id="loginAction" class="test.LoginAction" scope="prototype">

</bean>

这里,咱们把这个bean的id设为loginAction。Scope设为prototype,含义是每一次请求建立一个LoginAction类的实例,Scope还有另外一个值“singleton”意为“单例模式”。 

接下来修改struts.xml文件,把原来login.action的配置作以下修改:

把<action name="login" class=" test.LoginAction ">

改成

<action name="login" class="loginAction">

注意到有什么区别了吗?class值设为了loginAction,即LoginAction类的bean的ID。这样咱们就把LoginAction类交给了spring管理。至于具体是怎么处理的,秘密在struts2-spring-plugin-2.1.6.jar中,有空本身就去研究吧,如今会用就能够了。 

5.4 整合Hibernate

       Spring整合Hibernate主要是对hibernate的Session进行管理,包含Session的建立、提交、关闭的整个生命周期。Spring对事务的管理应用了AOP的技术,配置前请先了解一下AOP的知识。

1)        配置sessionFactory,让spring来建立Session。在applicationContext.xml中增长以下代码:

<!-- 配置sessionFactory -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="configLocation">
         <value>classpath:spring/hibernate.cfg.xml</value>
     </property>   
 </bean>    

       咱们原来是用HibernateSessionFactory.java来建立Session的,如今删除便可,交给Spring建立。这里,建立了一个Session工厂类的Bean,其ID为“sessionFactory”。

2)        配置事务管理器。增长以下代码:

 <!-- 配置事务管理器 -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory">
         <ref bean="sessionFactory"/>
     </property>   
 </bean>

       这里建立了一个id为transactionManager的事务管理器,它匹配一个session工厂,<ref bean="sessionFactory"/>这个sessionFactory是指session工厂的ID。

3)        对事务管理器进行事务设置。增长以下代码:

<tx:advice id="smAdvice" transaction-manager="transactionManager">
     <tx:attributes>
         <tx:method name="save*" propagation="REQUIRED"/>
         <tx:method name="del*" propagation="REQUIRED"/>
         <tx:method name="update*" propagation="REQUIRED"/>
     </tx:attributes>
 </tx:advice>

 

       这里建立了一个advice(通知),对事务管理器进行事务设置,这里意思是指,对于以save、del、update开头的方法应用事务。

4)        下面就把事务应用到具体的类。看以下代码:

<aop:config>
     <aop:pointcut id="smMethod" 
expression="execution(* test.service.impl.*.*(..))"/>
     <aop:advisor pointcut-ref="smMethod" advice-ref="smAdvice"/>
 </aop:config>

       这里配置的做用是把咱们上面建立的advice应用到具体的类中。以上代码的意思指,给test.service.impl下的全部类的全部方法应用smAdvice。

5)        示例:使用Session

     配置基本完毕了,本身去测试吧,这里就不先写了。

相关文章
相关标签/搜索