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

     配置基本完畢了,本身去測試吧,這裏就不先寫了。

相關文章
相關標籤/搜索