Spring經常使用知識點總結

1. Spring有哪些優勢?html

  輕量級:Spring在大小和透明性方面絕對屬於輕量級的,基礎版本的Spring框架大約只有2MB。
  控制反轉(IOC):Spring使用控制反轉技術實現了鬆耦合。依賴被注入到對象,而不是建立或尋找依賴對象。
  面向切面編程(AOP): Spring支持面向切面編程,同時把應用的業務邏輯與系統的服務分離開來。
  容器:Spring包含並管理應用程序對象的配置及生命週期。
  MVC框架:Spring的web框架是一個設計優良的web MVC框架,很好的取代了一些web框架。
  事務管理:Spring對下至本地業務上至全局業務(JAT)提供了統一的事務管理接口。
  異常處理:Spring提供一個方便的API將特定技術的異常(由JDBC, Hibernate, 或JDO拋出)轉化爲一致的、Unchecked異常。前端

 

2. Spring框架的基本模塊以下所示:java

  Core module, Bean module, Context module, Expression Language module, JDBC module,  ORM module, OXM module
  Java Messaging Service(JMS) module, Transaction module, Web module, Web-Servlet module, Web-Struts module, Web-Portlet module程序員

 

3. Spring IoC容器是什麼?
  Spring IOC負責建立對象、管理對象(經過依賴注入)、整合對象、配置對象以及管理這些對象的生命週期。web


4. Bean Factory和ApplicationContext有什麼區別?
  ApplicationContext提供了一種解決文檔信息的方法,一種加載文件資源的方式(如圖片),他們能夠向監聽他們的beans發送消息。另外,容器或者容器中beans的操做,這些必須以bean工廠的編程方式處理的操做能夠在應用上下文中以聲明的方式處理。應用上下文實現了MessageSource,該接口用於獲取本地消息,實際的實現是可選的。spring


5. Spring中的依賴注入是什麼?有哪些不一樣類型的IOC?區別是什麼?
  依賴注入做爲控制反轉(IOC)的一個層面,能夠有多種解釋方式。在這個概念中,你不用建立對象而只須要描述如何建立它們。你沒必要經過代碼直接的將組件和服務鏈接在一塊兒,而是經過配置文件說明哪些組件須要什麼服務。以後IOC容器負責銜接。
  構造器依賴注入:構造器依賴注入在容器觸發構造器的時候完成,該構造器一系列的參數,每一個參數表明注入的對象。
  Setter方法依賴注入:首先容器會觸發一個無參構造函數或無參靜態工廠方法實例化對象,以後容器調用bean中的setter方法完成Setter方法依賴注入。
  你能夠同時使用兩種方式的依賴注入,最好的選擇是使用構造器參數實現強制依賴注入,使用setter方法實現可選的依賴關係。數據庫

  (1) 構造器注入示例:express

public class AccountServiceImpl implements AccountService{
    private AccountDao accountDao; //須要注入的對象Dao層對象

    /**
 
     * 構造注入
 
     * @param accountDao
 
     */
    public AccountServiceImpl(AccountDao accountDao){
 
        this.accountDao=accountDao;
 
    }

    //........
  }
}

xml配置以下:
<bean name="accountDao" class="com.zejian.spring.springIoc.dao.impl.AccountDaoImpl"/>
<!-- 經過構造注入依賴 -->
<bean name="accountService" class="com.zejian.spring.springIoc.service.impl.AccountServiceImpl">
    <!-- 構造方法方式注入accountDao對象,-->
    <constructor-arg  ref="accountDao"/>
</bean>

  (2) setter方法注入:編程

  Setter注入顧名思義,被注入的屬性須要有set方法, Setter注入支持簡單類型和引用類型,Setter注入時在bean實例建立完成後執行的。直接觀察前面的案例,對象注入使用<property>的ref屬性。緩存

<!-- setter經過property 注入屬性值,普通類型使用value -->
<bean id="account" scope="prototype" class="com.zejian.spring.springIoc.pojo.Account" >
    <property name="name" value="I am SpringIOC1" />
    <property name="pwd" value="123" />
    <!-- 注入map -->
    <property name="books">
        <map>
          <entry key="10" value="CoreJava">
            </entry>
            <entry key="11" value="JavaWeb">
            </entry>
            <entry key="12" value="SSH2">
            </entry>
        </map>
  </property>
  <!-- 注入set -->
  <property name="friends">
       <set>
           <value>張龍</value>
           <value>老王</value>
           <value>王五</value>
       </set>
   </property>
  <!-- 注入list -->
   <property name="citys">
       <list>
           <value>北京</value>
           <value>上海</value>
           <value>深圳</value>
           <value>廣州</value>
       </list>
   </property>
</bean>

  

6. Spring應用程序看起來像什麼?
  1)一個定義功能的接口
  2)實現包括屬性,setter和getter方法,功能等
  3)Spring AOP
  4)Spring的XML配置文件
  5)使用該功能的客戶端編程


7. 什麼是Spring Beans?它定義了什麼內容?
  Spring Beans是構成Spring應用核心的Java對象。這些對象由Spring IOC容器實例化、組裝、管理。這些對象經過容器中配置的元數據建立,例如,使用XML文件中定義的建立。
在Spring中建立的beans都是單例的beans。在bean標籤中有一個屬性爲」singleton」,若是設爲true,該bean是單例的,若是設爲false,該bean是原型bean。Singleton屬性默認設置爲true。所以,spring框架中全部的bean都默認爲單例bean。
  Spring Bean中定義了全部的配置元數據,這些配置信息告知容器如何建立它,它的生命週期是什麼以及它的依賴關係。


8.如何向Spring 容器提供配置元數據?
  三種方式向Spring 容器提供元數據:
  XML配置文件
  基於註解配置
  基於Java的配置


9. 如何定義bean的做用域?Spring中支持的bean做用域有哪幾種?
  在bean定義的時候經過’scope’屬性定義便可。
  例如,當Spring須要產生每次一個新的bean實例時,應該聲明bean的scope屬性爲prototype。若是每次你但願Spring返回一個實例,應該聲明bean的scope屬性爲singleton。
  Spring框架支持以下五種不一樣的做用域:
    singleton:在Spring IOC容器中僅存在一個Bean實例,Bean以單實例的方式存在。
    prototype:一個bean能夠定義多個實例。
    request:每次HTTP請求都會建立一個新的Bean。該做用域僅適用於WebApplicationContext環境。
    session:一個HTTP Session定義一個Bean。該做用域僅適用於WebApplicationContext環境.
    globalSession:同一個全局HTTP Session定義一個Bean。該做用域一樣僅適用於WebApplicationContext環境.
  bean默認的scope屬性是’singleton‘。


10. 解釋Spring框架中bean的生命週期
  Spring容器讀取XML文件中bean的定義並實例化bean。Spring根據bean的定義設置屬性值。實例化一個Bean,也就是咱們一般說的new一個對象實例,

    1)按照Spring上下文對實例化的Bean進行配置,也就是IOC注入;
    2)若是這個Bean實現了BeanNameAware接口,會調用它實現的setBeanName(String beanId)方法,此處傳遞的是Spring配置文件中Bean的ID
    3)若是這個Bean實現了BeanFactoryAware接口,會調用它實現的setBeanFactory(),傳遞的是Spring工廠自己(能夠用這個方法獲取到其餘Bean)
    4)若是這個Bean實現了ApplicationContextAware接口,會調用setApplicationContext(ApplicationContext)方法,傳入Spring上下文,該方式一樣能夠實現步驟4,但比4更好,覺得ApplicationContext是BeanFactory的子接口,有更多的實現方法
    5)若是這個Bean關聯了BeanPostProcessor接口,將會調用postProcessBeforeInitialization(Object obj, String s)方法,BeanPostProcessor常常被用做是Bean內容的更改,而且因爲這個是在Bean初始化結束時調用After方法,也可用於內存或緩存技術
    6)若是這個Bean在Spring配置文件中配置了init-method屬性會自動調用其配置的初始化方法
    7)若是這個Bean關聯了BeanPostProcessor接口,將會調用postAfterInitialization(Object obj, String s)方法
     注意:以上工做完成之後就能夠用這個Bean了,那這個Bean是一個single的,因此通常狀況下咱們調用同一個ID的Bean會是在內容地址相同的實例

    8)當Bean再也不須要時,會通過清理階段,若是Bean實現了DisposableBean接口,會調用其實現的destroy方法

    9)最後,若是這個Bean的Spring配置中配置了destroy-method屬性,會自動調用其配置的銷燬方法。

 

11. 什麼是bean自動裝配?自動裝配有哪幾種模式?

  Spring容器能夠自動配置相互協做beans之間的關聯關係。這意味着Spring能夠自動配置一個bean和其餘協做bean之間的關係,經過檢查BeanFactory 的內容裏沒有使用和< property>元素。
  自動裝配提供五種不一樣的模式供Spring容器用來自動裝配beans之間的依賴注入:
  1)no:默認的方式是不進行自動裝配,經過手工設置ref 屬性來進行裝配bean。
  2)byName:經過參數名自動裝配,Spring容器查找beans的屬性,這些beans在XML配置文件中被設置爲byName。以後容器試圖匹配、裝配和該bean的屬性具備相同名字的bean。
  3)byType:經過參數的數據類型自動自動裝配,Spring容器查找beans的屬性,這些beans在XML配置文件中被設置爲byType。以後容器試圖匹配和裝配和該bean的屬性類型同樣的bean。若是有多個bean符合條件,則拋出錯誤。
  4)constructor:這個同byType相似,不過是應用於構造函數的參數。若是在BeanFactory中不是剛好有一個bean與構造函數參數相同類型,則拋出一個嚴重的錯誤。
  5)autodetect:若是默認的構造方法,經過 construct的方式自動裝配,不然使用 byType的方式自動裝配。
自動裝配以下侷限性:
  重寫:你仍然須要使用 和< property>設置指明依賴,這意味着總要重寫自動裝配。
  原生數據類型:你不能自動裝配簡單的屬性,如原生類型、字符串和類。
  模糊特性:自動裝配老是沒自定義裝配精確,所以,若是可能儘可能使用自定義裝配。


12. Spring支持的事務管理類型有哪些?優勢是什麼?
  Spring支持以下兩種方式的事務管理:
    1)編程式事務管理:這意味着你能夠經過編程的方式管理事務,這種方式帶來了很大的靈活性,但很難維護。
    2)聲明式事務管理:這種方式意味着你能夠將事務管理和業務代碼分離。你只須要經過註解或者XML配置管理事務。
  有以下優勢:
    1)它爲不一樣的事務API(如JTA, JDBC, Hibernate, JPA, 和JDO)提供了統一的編程模型。
    2)它爲編程式事務管理提供了一個簡單的API而非一系列複雜的事務API(如JTA).
    3)它支持聲明式事務管理。
    4)它能夠和Spring 的多種數據訪問技術很好的融合。

  兩種事務管理類型區別:

    編程式事務容許用戶在代碼中精肯定義事務的邊界,而聲明式事務(基於AOP)有助於用戶將操做與事務規則進行解耦。 

    簡單地說,編程式事務侵入到了業務代碼裏面,可是提供了更加詳細的事務管理;而聲明式事務因爲基於AOP,因此既能起到事務管理的做用,又能夠不影響業務代碼的具體實現。


13. 什麼是Spring的AOP?
  AOP即面向切面編程, 它容許程序員模塊化橫向業務邏輯,或定義核心部分的功能,例如日誌管理和事務管理。
  (切面:一種新的模塊化機制,用來描述分散在對象、類或方法中的橫切關注點)
  (橫切關注: 是會影響到整個應用程序的關注功能,它跟正常的業務邏輯是正交的,沒有必然的聯繫,可是幾乎全部的業務邏輯都會涉及到這些關注功能。一般,事務、日誌、安全性等關注就是應用中的橫切關注功能。)


14. 在Spring AOP中concern和 cross-cutting concern的區別是什麼?
  Concern(核心邏輯):表示在應用程序中一個模塊的行爲。Concern能夠定義爲咱們想要實現的功能。
  Cross-cutting concern(橫向的通用邏輯):指的是整個應用程序都會用到的功能,它影響整個應用程序。例如,日誌管理(Logging)、安全管理(Security)以及數據交互是應用程序的每一個模塊都要涉及到的,所以這些都屬於Cross-cutting concern。


15. Spring切面有幾種類型的通知?
  1)before(前置通知):在一個方法以前執行的通知。
  2)after(最終通知):當某鏈接點退出的時候執行的通知(不管是正常返回仍是異常退出)。
  3)after-returning(後置通知):在某鏈接點正常完成後執行的通知。
  4)after-throwing(異常通知):在方法拋出異常退出時執行的通知。
  5)around(環繞通知):在方法調用先後觸發的通知。


16. 什麼是代理?有幾種不一樣類型的自動代理?
  代理是將通知應用到目標對象後建立的對象。從客戶端的角度看,代理對象和目標對象是同樣的。
  代理有以下幾種:
    1)BeanNameAutoProxyCreator:bean名稱自動代理建立器
    2)DefaultAdvisorAutoProxyCreator:默認通知者自動代理建立器
    3)Metadata autoproxying:元數據自動代理


17. 什麼是織入?
  織入是將切面和其餘應用類型或對象鏈接起來建立一個通知對象的過程。織入能夠在編譯、加載或運行時完成。


 18. 什麼是IoC和DI?DI是如何實現的?
  IoC叫控制反轉,是Inversion of Control的縮寫,DI(Dependency Injection)叫依賴注入,是對IoC更簡單的詮釋。
  控制反轉是把傳統上由程序代碼直接操控的對象的調用權交給容器,經過容器來實現對象組件的裝配和管理。所謂的」控制反轉」就是對組件對象控制權的轉移,從程序代碼自己轉移到了外部容器,由容器來建立對象並管理對象之間的依賴關係。IoC體現了好萊塢原則 – 「Don’t call me, we will call you」。
  依賴注入的基本原則是應用組件不該該負責查找資源或者其餘依賴的協做對象。配置對象的工做應該由容器負責,查找資源的邏輯應該從應用組件的代碼中抽取出來,交給容器來完成。DI是對IoC更準確的描述,即組件之間的依賴關係由容器在運行期決定,形象的來講,即由容器動態的將某種依賴關係注入到組件之中。
  依賴注入能夠經過setter方法注入(設值注入)、構造器注入和接口注入三種方式來實現,Spring支持setter注入和構造器注入,一般使用構造器注入來注入必須的依賴關係,對於可選的依賴關係,則setter注入是更好的選擇,setter注入須要類提供無參構造器或者無參的靜態工廠方法來建立對象。


19. 依賴注入時如何注入集合屬性?
答:能夠在定義Bean屬性時,經過<list> / <set> / <map> / <props>分別爲其注入列表、集合、映射和鍵值都是字符串的映射屬性。


20. Spring中的自動裝配有哪些限制?
  1)若是使用了構造器注入或者setter注入,那麼將覆蓋自動裝配的依賴關係。
  2)基本數據類型的值、字符串字面量、類字面量沒法使用自動裝配來注入。
  3)優先考慮使用顯式的裝配來進行更精確的依賴注入而不是使用自動裝配。

 

21. Spring的啓動過程:

(1)對於一個web應用,其部署在web容器中,web容器提供其一個全局的上下文環境,這個上下文就是ServletContext,其爲後面的spring IoC容器提供宿主環境;

(2)在web.xml中會提供有contextLoaderListener。在web容器啓動時,會觸發容器初始化事件,此時contextLoaderListener會監聽到這個事件,其contextInitialized方法會被調用,在這個方法中,spring會初始化一個啓動上下文,這個上下文被稱爲根上下文,即WebApplicationContext,這是一個接口類,確切的說,其實際的實現類是XmlWebApplicationContext。這個就是spring的IoC容器,其對應的Bean定義的配置由web.xml中的context-param標籤指定。  

  在這個IoC容器初始化完畢後,spring以WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE爲屬性Key,將其存儲到ServletContext中,便於獲取;
(3)contextLoaderListener監聽器初始化完畢後,開始初始化web.xml中配置的Servlet,這個servlet能夠配置多個,以最多見的DispatcherServlet爲例,這個servlet其實是一個標準的前端控制器,用以轉發、匹配、處理每一個servlet請求。

  DispatcherServlet上下文在初始化的時候會創建本身的IoC上下文,用以持有spring mvc相關的bean。在創建DispatcherServlet本身的IoC上下文時,會利用WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE先從ServletContext中獲取以前的根上下文(即WebApplicationContext)做爲本身上下文的parent上下文。有了這個parent上下文以後,再初始化本身持有的上下文。大概的工做就是初始化處理器映射、視圖解析等。這個servlet本身持有的上下文默認實現類也是XmlWebApplicationContext。

(4)初始化完畢後,spring以與servlet的名字相關(此處不是簡單的以servlet名爲Key,而是經過一些轉換,具體可自行查看源碼)的屬性爲屬性Key,也將其存到ServletContext中,以便後續使用。這樣每一個servlet就持有本身的上下文,即擁有本身獨立的bean空間,同時各個servlet共享相同的bean,即根上下文(第2步中初始化的上下文)定義的那些bean。

 

22. contextLoaderListener監聽容器初始化事件時,首先會在ContextLoaderListener中經過<context-param>中的applicationContext.xml建立一個ApplicationContext,再將這個ApplicationContext塞到ServletContext裏面,爲整個Web應用程序所共享。DispatcherServlet會維持一個本身的ApplicationContext,默認會讀取/WEB-INFO/<dispatcherServletName>-servlet.xml文件,

那麼,以上兩個ApplicationContext的關係是什麼,它們的做用做用範圍分別是什麼,它們的用途分別是什麼?

(1)ContextLoaderListener中建立ApplicationContext主要用於整個Web應用程序須要共享的一些組件,好比DAO,數據庫的ConnectionFactory等。而由DispatcherServlet建立的ApplicationContext主要用於和該Servlet相關的一些組件,好比Controller、ViewResovler等。對於做用範圍而言,在DispatcherServlet中能夠引用由ContextLoaderListener所建立的ApplicationContext,而反過來不行。

(2)在Spring的具體實現上,這兩個ApplicationContext都是經過ServletContext的setAttribute方法放到ServletContext中的。可是,ContextLoaderListener會先於DispatcherServlet建立ApplicationContext,DispatcherServlet在建立ApplicationContext時會先找到由ContextLoaderListener所建立的ApplicationContext,再將後者的ApplicationContext做爲參數傳給DispatcherServlet的ApplicationContext的setParent()方法。

(3)當Spring在執行ApplicationContext的getBean時,若是在本身context中找不到對應的bean,則會在父ApplicationContext中去找。這也解釋了爲何咱們能夠在DispatcherServlet中獲取到由ContextLoaderListener對應的ApplicationContext中的bean。

 

23. Spring的事務處理機制?

 Spring事務管理器的接口是org.springframework.transaction.PlatformTransactionManager,經過這個接口,Spring爲各個平臺如JDBC、Hibernate等都提供了對應的事務管理器,具體實現由各平臺本身來作。此接口的內容以下:

Public interface PlatformTransactionManager()...{  
    // 由TransactionDefinition獲得TransactionStatus對象
    TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException; 
    // 提交
    Void commit(TransactionStatus status) throws TransactionException;  
    // 回滾
    Void rollback(TransactionStatus status) throws TransactionException;  
} 

  

Spring定義了以下七種傳播行爲:

傳播行爲 含義
PROPAGATION_REQUIRED 表示當前方法必須運行在事務中。若是當前事務存在,方法將會在該事務中運行。不然,會啓動一個新的事務
PROPAGATION_SUPPORTS 表示當前方法不須要事務上下文,可是若是存在當前事務的話,那麼該方法會在這個事務中運行
PROPAGATION_MANDATORY 表示該方法必須在事務中運行,若是當前事務不存在,則會拋出一個異常
PROPAGATION_REQUIRED_NEW 表示當前方法必須運行在它本身的事務中。一個新的事務將被啓動。若是存在當前事務,在該方法執行期間,當前事務會被掛起。若是使用JTATransactionManager的話,則須要訪問TransactionManager
PROPAGATION_NOT_SUPPORTED 表示該方法不該該運行在事務中。若是存在當前事務,在該方法運行期間,當前事務將被掛起。若是使用JTATransactionManager的話,則須要訪問TransactionManager
PROPAGATION_NEVER 表示當前方法不該該運行在事務上下文中。若是當前正有一個事務在運行,則會拋出異常
PROPAGATION_NESTED 表示若是當前已經存在一個事務,那麼該方法將會在嵌套事務中運行。嵌套的事務能夠獨立於當前事務進行單獨地提交或回滾。若是當前事務不存在,那麼其行爲與PROPAGATION_REQUIRED同樣。注意各廠商對這種傳播行爲的支持是有所差別的。能夠參考資源管理器的文檔來確認它們是否支持嵌套事務

 

24. 如何實現編程式事務?

Spring提供兩種方式的編程式事務管理,分別是:使用TransactionTemplate和直接使用PlatformTransactionManager。
(1) 使用TransactionTemplate
採用TransactionTemplate和採用其餘Spring模板,如JdbcTempalte和HibernateTemplate是同樣的方法。它使用回調方法,把應用程序從處理取得和釋放資源中解脫出來。如同其餘模板,TransactionTemplate是線程安全的。代碼片斷:

TransactionTemplate tt = new TransactionTemplate(); 
    Object result = tt.execute(
        new TransactionCallback(){
            public Object doTransaction(TransactionStatus status){
                updateOperation();
                return resultOfUpdateOperation();
            }
}); // 執行execute方法進行事務管理

  使用TransactionCallback()能夠返回一個值。若是使用TransactionCallbackWithoutResult則沒有返回值。

(2) 使用PlatformTransactionManager
示例代碼以下:

DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); //定義一個某個框架平臺的TransactionManager,如JDBC、Hibernate
    dataSourceTransactionManager.setDataSource(this.getJdbcTemplate().getDataSource()); // 設置數據源
    DefaultTransactionDefinition transDef = new DefaultTransactionDefinition(); // 定義事務屬性
    transDef.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED); // 設置傳播行爲屬性
    TransactionStatus status = dataSourceTransactionManager.getTransaction(transDef); // 得到事務狀態
    try {
        // 數據庫操做
        dataSourceTransactionManager.commit(status);// 提交
    } catch (Exception e) {
        dataSourceTransactionManager.rollback(status);// 回滾
 }

  

25. 如何實現聲明式事務?

根據代理機制的不一樣,總結了五種Spring事務的配置方式,配置文件以下:

(1)每一個Bean都有一個代理

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>

    <!-- 定義事務管理器(聲明式的事務) -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- 配置DAO -->
    <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="userDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
           <!-- 配置事務管理器 -->
         <property name="transactionManager" ref="transactionManager" />
         <property name="target" ref="userDaoTarget" />
         <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
        <!-- 配置事務屬性 -->
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
</beans>

  (2)全部Bean共享一個代理基類

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>

    <!-- 定義事務管理器(聲明式的事務) -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="transactionBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
            lazy-init="true" abstract="true">
        <!-- 配置事務管理器 -->
        <property name="transactionManager" ref="transactionManager" />
        <!-- 配置事務屬性 -->
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <!-- 配置DAO -->
    <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="userDao" parent="transactionBase" >
        <property name="target" ref="userDaoTarget" />
    </bean>
</beans>

  (3)使用攔截器

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>

    <!-- 定義事務管理器(聲明式的事務) -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="transactionInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager" ref="transactionManager" />
        <!-- 配置事務屬性 -->
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
                <value>*Dao</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>

    <!-- 配置DAO -->
    <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

  (4)使用tx標籤配置的攔截器

<?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:context="http://www.springframework.org/schema/context"
    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/context
           http://www.springframework.org/schema/context/spring-context-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">

    <context:annotation-config />
    <context:component-scan base-package="com.bluesky" />

    <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>

    <!-- 定義事務管理器(聲明式的事務) -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="interceptorPointCuts"
            expression="execution(* com.bluesky.spring.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice"
            pointcut-ref="interceptorPointCuts" />
    </aop:config>
</beans>

  (5)全註解

<?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:context="http://www.springframework.org/schema/context"
    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/context
           http://www.springframework.org/schema/context/spring-context-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">

    <context:annotation-config />
    <context:component-scan base-package="com.bluesky" />

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>

    <!-- 定義事務管理器(聲明式的事務) -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

  此時在DAO上需加上@Transactional註解,以下:

@Transactional
@Component("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    public List<User> listUsers() {
        return this.getSession().createQuery("from User").list();
    }
}

  

 

參考博文:

http://www.importnew.com/11657.html
https://www.jianshu.com/p/5b8c9c09b294

http://www.mamicode.com/info-detail-1248286.html

相關文章
相關標籤/搜索