前段時(shí)間對(duì)spring的事務(wù)配置做了比較深入的研究,在此之間對(duì)Spring的事務(wù)配置雖說也配置過,但是一直沒有一個(gè)清楚的認(rèn)識(shí)褪秀。通過這次的學(xué)習(xí)發(fā)覺Spring的事務(wù)配置只要把思路理清,還是比較好掌握的。
總結(jié)如下:
Spring配置文件中關(guān)于事務(wù)配置總是由三個(gè)組成部分邢隧,分別是DataSource、TransactionManager和代理機(jī)制這三部分冈在,無論哪種配置方式倒慧,一般變化的只是代理機(jī)制這部分。
DataSource包券、TransactionManager這兩部分只是會(huì)根據(jù)數(shù)據(jù)訪問方式有所變化纫谅,比如使用hibernate進(jìn)行數(shù)據(jù)訪問時(shí),DataSource實(shí)際為SessionFactory溅固,TransactionManager的實(shí)現(xiàn)為HibernateTransactionManager付秕。
根據(jù)代理機(jī)制的不同,總結(jié)了五種Spring事務(wù)的配置方式侍郭,配置文件如下:
第一種方式:每個(gè)Bean都有一個(gè)代理
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">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
PROPAGATION_REQUIRED
第二種方式:所有Bean共享一個(gè)代理基類
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">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true"abstract="true">
PROPAGATION_REQUIRED
第三種方式:使用攔截器
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">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
class="org.springframework.transaction.interceptor.TransactionInterceptor">
PROPAGATION_REQUIRED
*Dao
transactionInterceptor
第四種方式:使用tx標(biāo)簽配置的攔截器
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">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
expression="execution(* com.bluesky.spring.dao.*.*(..))"/>
pointcut-ref="interceptorPointCuts"/>
第五種方式:全注解
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">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
此時(shí)在DAO上需加上@Transactional注解询吴,如下:
packagecom.bluesky.spring.dao;
importjava.util.List;
importorg.hibernate.SessionFactory;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
importorg.springframework.stereotype.Component;
importcom.bluesky.spring.domain.User;
@Transactional
@Component("userDao")
publicclassUserDaoImplextendsHibernateDaoSupportimplementsUserDao {
publicListlistUsers() {
returnthis.getSession().createQuery("from User").list();
}
}