前段時間對Spring的事務配置做了比較深入的研究在此之間對Spring的事務配置雖說也配置過但是一直沒有一個清楚的認識通過這次的學習發覺Spring的事務配置只要把思路理清還是比較好掌握的
總結如下
Spring配置文件中關於事務配置總是由三個組成部分分別是DataSourceTransactionManager和代理機制這三部分無論哪種配置方式一般變化的只是代理機制這部分
DataSourceTransactionManager這兩部分只是會根據數據訪問方式有所變化比如使用Hibernate進行數據訪問時DataSource實際為SessionFactoryTransactionManager的實現為HibernateTransactionManager
具體如下圖
根據代理機制的不同總結了五種Spring事務的配置方式配置文件如下
第一種方式每個Bean都有一個代理
<?xml version=
encoding=
UTF
?>
<beans xmlns=
xmlns:xsi=
instance
xmlns:context=
xmlns:aop=
xsi:schemaLocation=
beans
xsd
context
xsd
aop
xsd
>
<bean id=
sessionFactory
class=
org
springframework
orm
hibernate
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
hibernate
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>
第二種方式所有Bean共享一個代理基類
<?xml version=
encoding=
UTF
?>
<beans xmlns=
xmlns:xsi=
instance
xmlns:context=
xmlns:aop=
xsi:schemaLocation=
beans
xsd
context
xsd
aop
xsd
>
<bean id=
sessionFactory
class=
org
springframework
orm
hibernate
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
hibernate
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>
第三種方式使用攔截器
<?xml version=
encoding=
UTF
?>
<beans xmlns=
xmlns:xsi=
instance
xmlns:context=
xmlns:aop=
xsi:schemaLocation=
beans
xsd
context
xsd
aop
xsd
>
<bean id=
sessionFactory
class=
org
springframework
orm
hibernate
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
hibernate
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>
第四種方式使用tx標簽配置的攔截器
<?xml version=
encoding=
UTF
?>
<beans xmlns=
xmlns:xsi=
instance
xmlns:context=
xmlns:aop=
xmlns:tx=
xsi:schemaLocation=
beans
xsd
context
xsd
aop
xsd
tx
xsd
>
<context:annotation
config />
<context:component
scan base
package=
com
bluesky
/>
<bean id=
sessionFactory
class=
org
springframework
orm
hibernate
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
hibernate
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>
第五種方式全注解
<?xml version=
encoding=
UTF
?>
<beans xmlns=
xmlns:xsi=
instance
xmlns:context=
xmlns:aop=
xmlns:tx=
xsi:schemaLocation=
beans
xsd
context
xsd
aop
xsd
tx
xsd
>
<context:annotation
config />
<context:component
scan base
package=
com
bluesky
/>
<tx:annotation
driven transaction
manager=
transactionManager
/>
<bean id=
sessionFactory
class=
org
springframework
orm
hibernate
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
hibernate
HibernateTransactionManager
>
<property name=
sessionFactory
ref=
sessionFactory
/>
</bean>
</beans>
此時在DAO上需加上@Transactional注解如下
package com
bluesky
spring
dao;
import java
util
List;
import org
hibernate
SessionFactory;
import org
springframework
beans
factory
annotation
Autowired;
import org
springframework
orm
hibernate
support
HibernateDaoSupport;
import org
springframework
stereotype
Component;
import com
bluesky
spring
domain
User;
@Transactional
@Component(
userDao
)
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public List<User> listUsers() {
return this
getSession()
createQuery(
from User
)
list();
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28887.html