Spring容器中有兩種思想很重要,也就是我們常用的Ioc和Aop,如果理解了這兩種思想,對(duì)于我們學(xué)習(xí)設(shè)計(jì)模式和編程有很大的幫助,美國四人幫(GOF)寫的設(shè)計(jì)模式中,有很多都用到了Ioc的思想。簡(jiǎn)單的說就是依賴注入的思想。常見的一種情況:如果一個(gè)類中要復(fù)用另外一個(gè)類中的功能時(shí),我們可能會(huì)首先想到繼承,如果你知道Ioc這種思想的話,我想你不會(huì)用繼承,你會(huì)馬上想到把要用到功能抽取出來,在我們要用到的類中只需通過set方法簡(jiǎn)單的注入就可以了,其實(shí)這里用到了對(duì)象的組合代替繼承,這樣不僅避免了單一繼承,還很好的實(shí)現(xiàn)了松耦合。同時(shí)也遵循了面向?qū)ο蟮木幊痰脑O(shè)計(jì)原則:多用組合,少用繼承。在這里對(duì)于Ioc和Aop這兩種思想的好處。我就不介紹了。接下來我要說的是Spring中幾種常見的事務(wù)配置,是Aop和Ioc的充分體現(xiàn)。
--------------------------------------
一、Propagation :
對(duì)于特定的方法或方法命名模式,代理的具體事務(wù)行為由事務(wù)屬性驅(qū)動(dòng),如下面的例子所示:
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
key屬性確定代理應(yīng)該給哪個(gè)方法增加事務(wù)行為。這樣的屬性最重要的部份是傳播行為。有以下選項(xiàng)可供使用:
PROPAGATION_REQUIRED--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就新建一個(gè)事務(wù)。這是最常見的選擇。
PROPAGATION_SUPPORTS--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就以非事務(wù)方式執(zhí)行。
PROPAGATION_MANDATORY--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就拋出異常。
PROPAGATION_REQUIRES_NEW--新建事務(wù),如果當(dāng)前存在事務(wù),把當(dāng)前事務(wù)掛起。
PROPAGATION_NOT_SUPPORTED--以非事務(wù)方式執(zhí)行操作,如果當(dāng)前存在事務(wù),就把當(dāng)前事務(wù)掛起。
PROPAGATION_NEVER--以非事務(wù)方式執(zhí)行,如果當(dāng)前存在事務(wù),則拋出異常。
????? 前六個(gè)策略類似于EJB CMT:常量名相同,因此,對(duì)EJB開發(fā)人員來說,應(yīng)該立刻就感到熟悉。第七個(gè)(PROPAGATION_NESTED)是Spring所提供的一個(gè)特殊變量。它要求事務(wù)管理器或者使用JDBC 3.0 Savepoint API提供嵌套事務(wù)行為(如Spring的DataSourceTransactionManager),或者通過JTA支持嵌套事務(wù)。
二、Isolation Level(事務(wù)隔離等級(jí)):
1、Serializable:最嚴(yán)格的級(jí)別,事務(wù)串行執(zhí)行,資源消耗最大;
2、REPEATABLE READ:保證了一個(gè)事務(wù)不會(huì)修改已經(jīng)由另一個(gè)事務(wù)讀取但未提交(回滾)的數(shù)據(jù)。避免了“臟讀取”和“不可重復(fù)讀取”的情況,但是帶來了更多的性能損失。
3、READ COMMITTED:大多數(shù)主流數(shù)據(jù)庫的默認(rèn)事務(wù)等級(jí),保證了一個(gè)事務(wù)不會(huì)讀到另一個(gè)并行事務(wù)已修改但未提交的數(shù)據(jù),避免了“臟讀取”。該級(jí)別適用于大多數(shù)系統(tǒng)。
4、Read Uncommitted:保證了讀取過程中不會(huì)讀取到非法數(shù)據(jù)。
spring中的Isolation屬性:
1、ISOLATION_DEFAULT :使用當(dāng)前數(shù)據(jù)源的默認(rèn)級(jí)別
2、ISOLATION_READ_UNCOMMITTED :Dirty reads, non-repeatable reads, and phantom reads can occur.
3、ISOLATION_READ_COMMITTED
irty reads are prevented; non-repeatable reads and phantom reads can occur.
4、ISOLATION_REPEATABLE_READ:Dirty reads and non-repeatable reads are prevented; phantom reads can occur.
5、ISOLATION_SERIALIZABLE:Dirty reads, non-repeatable reads, and phantom reads are prevented.
三、readOnly
事務(wù)屬性中的readOnly標(biāo)志表示對(duì)應(yīng)的事務(wù)應(yīng)該被最優(yōu)化為只讀事務(wù)。這是一個(gè)最優(yōu)化提示。在一些情況下,一些事務(wù)策略能夠起到顯著的最優(yōu)化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)時(shí)避免dirty checking(試圖“刷新”)。
四、Timeout
????? 在事務(wù)屬性中還有定義“timeout”值的選項(xiàng),指定事務(wù)超時(shí)為幾秒。在JTA中,這將被簡(jiǎn)單地傳遞到J2EE服務(wù)器的事務(wù)協(xié)調(diào)程序,并據(jù)此得到相應(yīng)的解釋。
事務(wù)劃分策略
1、推薦在業(yè)務(wù)層使用事務(wù),這樣可以允許業(yè)務(wù)層捕獲導(dǎo)致rollback的異常,并拋出恰當(dāng)?shù)臉I(yè)務(wù)層異常;不在dao層使用事務(wù)是因?yàn)檫@會(huì)限制了dao重用其他事務(wù)需求,并且dao層沒有實(shí)現(xiàn)業(yè)務(wù)邏輯,并且原子性也是業(yè)務(wù)層的概念。
spring聲明性事務(wù)的劃分:
1、有四個(gè)地方需要配置:The four participants are transaction manager, proxy factory, transaction interceptor, and a set of transaction attributes.
2、使用ProxyFactoryBean/Transaction Interceptor(transactionInterceptor)配置spring事務(wù)
以下為配置實(shí)例:
<!-- The DBCP DataSource -->
? <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"?
??????? destroy-method="close">
??? <property name="driverClassName">
????? <value>${jdbc.driverClassName}</value>
??? </property>
??? <property name="url"><value>${jdbc.url}</value></property>
??? <property name="username"><value>${jdbc.username}</value></property>
??? <property name="password"><value>${jdbc.password}</value></property>
? </bean>
??
? <!-- The DAO class -->
? <bean id="dao"
?? class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- The transactionmanager to use for regular non JTA datasource -->
? <bean id="transactionManager"????? class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- TransactionInterceptor -->
? <bean id="transactionInterceptor"??????? class="org.springframework.transaction.interceptor.TransactionInterceptor">??? <property name="transactionManager">????? <ref bean="transactionManager"/>??? </property>??? <property name="transactionAttributeSource">????? <value>org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,readOnlyorg.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIRED
????? </value>
??? </property>
? </bean>?
??
? <!-- Transactional proxy for the primary business object -->
? <bean id="boxOffice"??????????? class="org.springframework.aop.framework.ProxyFactoryBean">??? <property name="target">????? <ref local="boxOfficeTarget"/>??? </property>??? <property name="proxyInterfaces">????? <value>org.springframework.prospring.ticket.service.BoxOffice</value>??? </property>??? <property name="interceptorNames">????? <value>transactionInterceptor</value>??? </property>
? </bean>?
??
? <!-- Business Object -->
? <bean id="boxOfficeTarget"
??? class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
??? <property name="boxOfficeDao">
????? <ref local="dao"/>
??? </property>
? </bean>
3、使用TransactionProxyFactoryBean配置spring事務(wù)
以下為配置實(shí)例:
? <!-- The DBCP DataSource -->
? <bean id="dataSource"???????? class="org.apache.commons.dbcp.BasicDataSource"
??????? destroy-method="close">
??? <property name="driverClassName">
????? <value>${jdbc.driverClassName}</value>
??? </property>
??? <property name="url"><value>${jdbc.url}</value></property>
??? <property name="username"><value>${jdbc.username}</value></property>
??? <property name="password"><value>${jdbc.password}</value></property>
? </bean>
??
? <!-- The DAO class -->
? <bean id="dao"
class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- The transactionmanager to use for regular non JTA datasource -->
? <bean id="transactionManager"??? class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- Transactional proxy and the primary business object -->
? <bean id="boxOffice"?????? class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">??? <property name="transactionManager"><ref bean="transactionManager"/></property>??? <property name="target">????? <bean class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
??????? <property name="boxOfficeDao">
????????? <ref local="dao"/>
??????? </property>
????? </bean>
??? </property>
??? <property name="transactionAttributes">??????????? <props>???????????????? <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>???????????????? <prop key="allocate*">PROPAGATION_REQUIRED</prop>??????????? </props>?????? </property>
? </bean>?
4、使用BeanNameAutoProxyCreator配置spring事務(wù)
如果有大量的bean需要使用事物,那么只要在配置文件中提供bean name給BeanNameAutoProxyCreator,spring就會(huì)個(gè)給該bean提供事務(wù)代理,配置實(shí)例如下:
? <!-- The DBCP DataSource -->
? <bean id="dataSource"??? class="org.apache.commons.dbcp.BasicDataSource"
????? destroy-method="close">
??? <property name="driverClassName">
????? <value>${jdbc.driverClassName}</value>
??? </property>
??? <property name="url"><value>${jdbc.url}</value></property>
??? <property name="username"><value>${jdbc.username}</value></property>
??? <property name="password"><value>${jdbc.password}</value></property>
? </bean>
??
? <!-- The DAO class -->
? <bean id="dao"
?? class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- The transactionmanager to use for regular non JTA datasource -->
? <bean id="transactionManager"?????? class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- TransactionInterceptor -->
? <bean id="transactionInterceptor"???????? class="org.springframework.transaction.interceptor.TransactionInterceptor">??? <property name="transactionManager">????? <ref bean="transactionManager"/>??? </property>??? <property name="transactionAttributeSource">????? <value>org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,readOnlyorg.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIRED????? </value>??? </property>
? </bean>?
??
? <!-- BeanNameAutoProxyCreator -->
<bean id="autoProxyCreator"??? class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">? <property name="interceptorNames">??? <value>transactionInterceptor</value>? </property>? <property name="beanNames">??? <list>????? <idref local="boxOffice"/>??? </list>? </property>
</bean>?
??
<!-- Business Object -->
<bean id="boxOffice"
?? class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
? <property name="boxOfficeDao">
??? <ref local="dao"/>
? </property>
</bean>
http://www.blogjava.net/280211429/articles/75529.html
-----------------------------------------------------
Spring AOP特性的聲明式事務(wù)管理,就可以避免編程式事務(wù)帶來的難維護(hù)性。聲明式事務(wù)只需要在配置文件中聲明即可。這樣的好處是業(yè)務(wù)邏輯(Dao)就不會(huì)意識(shí)到事務(wù)管理的存在,而且維護(hù)起來極其方便。
簡(jiǎn)化配置:
<bean id="TransactionProxyFactoryBean" lazy-init="true"?
??????? class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">??
??????? <property name="transactionManager">??
??????????? <ref bean="transactionManager" />??
??????? </property>??
??????? <property name="transactionAttributes">??
??????????? <props>??
??????????????? <prop key="do*">PROPAGATION_REQUIRED</prop>??
??????????????? <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>??
??????????? </props>??
??????? </property>??
??? </bean>??
?
<bean id="ForumService" parent="TransactionProxyFactoryBean">??
?????????? <property name="target">???
??????????? <bean class="ForumServiceImp">???
??????? </property>???
</bean>??
<bean id="ForumServiceImp" class="com.bbs.service.imp.ForumServiceImp">??
??????? <property name="forumDAO">??
??????????? <ref local="ForumDAO" />??
??????? </property>??
??? </bean>?
-------------------------------------------------------
TransactionProxyFactoryBean里的transactionAttributes,他的prop元素中的key屬性定義了target類的某個(gè)方法,可以使用通配符,如save*,prop元素的值定義了對(duì)key中定義方法應(yīng)用的事務(wù)執(zhí)行屬性,指定格式如下:
傳播行為,隔離等級(jí),只讀,+異常,-異常
其中傳播行為必須定義,其他可省,例如:
......
???? <prop key="xxxx">PROPAGATION_REQUIRED,readOnly,-DataAccessException</prop>
......
"-"表示發(fā)生指定異常后立即回滾,"+"表示發(fā)生指定異常后立即提交。
經(jīng)過上面xml定義,就相當(dāng)于在UserDaoImpl的saveOrUpdate方法前后加入了事務(wù)處理的代碼。
定義了userDaoProxy后,我們?cè)谙胧褂胾serDaoImpl,就需要注入userDaoProxy的bean了,而不是userDaoImpl的bean
? <property name="userDao">
??? <ref bean="userDaoProxy"/>
?? </property>
當(dāng)不想使用事務(wù)管理時(shí),把userController注入的userDaoProxy改為注入userDaoImpl就行了。
proxyTargetClass需要配置的情況只有一種:
? 當(dāng)TestTram類有實(shí)現(xiàn)某個(gè)接口,而TestProxyClass類中配置的類對(duì)象是TestTram時(shí)(而不是TestTram實(shí)現(xiàn)的接口),這時(shí)候你需要配置proxyTargetClass=true
?
? 如果TestTram沒有實(shí)現(xiàn)某個(gè)接口,而TestProxyClass類中配置的類對(duì)象是TestTram,這個(gè)時(shí)候我們是不需要配置proxyTargetClass=true的.(使用cgilib來動(dòng)態(tài)代理)
? 如果TestTram實(shí)現(xiàn)某個(gè)接口, 而TestProxyClass類中配置的是TestTram實(shí)現(xiàn)的interface的話.那樣我既不需要配置proxyInterface,也不需要配置proxyTargetClass
?
為什么我們?cè)跊]有配置proxyInterface情況下,去配置proxyTargetClass.因?yàn)閟pring會(huì)去拿到當(dāng)前配置的target實(shí)現(xiàn)的所有接口,然后通過動(dòng)態(tài)代理出類.
--------------------------------------
一、Propagation :
對(duì)于特定的方法或方法命名模式,代理的具體事務(wù)行為由事務(wù)屬性驅(qū)動(dòng),如下面的例子所示:
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
key屬性確定代理應(yīng)該給哪個(gè)方法增加事務(wù)行為。這樣的屬性最重要的部份是傳播行為。有以下選項(xiàng)可供使用:
PROPAGATION_REQUIRED--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就新建一個(gè)事務(wù)。這是最常見的選擇。
PROPAGATION_SUPPORTS--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就以非事務(wù)方式執(zhí)行。
PROPAGATION_MANDATORY--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就拋出異常。
PROPAGATION_REQUIRES_NEW--新建事務(wù),如果當(dāng)前存在事務(wù),把當(dāng)前事務(wù)掛起。
PROPAGATION_NOT_SUPPORTED--以非事務(wù)方式執(zhí)行操作,如果當(dāng)前存在事務(wù),就把當(dāng)前事務(wù)掛起。
PROPAGATION_NEVER--以非事務(wù)方式執(zhí)行,如果當(dāng)前存在事務(wù),則拋出異常。
????? 前六個(gè)策略類似于EJB CMT:常量名相同,因此,對(duì)EJB開發(fā)人員來說,應(yīng)該立刻就感到熟悉。第七個(gè)(PROPAGATION_NESTED)是Spring所提供的一個(gè)特殊變量。它要求事務(wù)管理器或者使用JDBC 3.0 Savepoint API提供嵌套事務(wù)行為(如Spring的DataSourceTransactionManager),或者通過JTA支持嵌套事務(wù)。
二、Isolation Level(事務(wù)隔離等級(jí)):
1、Serializable:最嚴(yán)格的級(jí)別,事務(wù)串行執(zhí)行,資源消耗最大;
2、REPEATABLE READ:保證了一個(gè)事務(wù)不會(huì)修改已經(jīng)由另一個(gè)事務(wù)讀取但未提交(回滾)的數(shù)據(jù)。避免了“臟讀取”和“不可重復(fù)讀取”的情況,但是帶來了更多的性能損失。
3、READ COMMITTED:大多數(shù)主流數(shù)據(jù)庫的默認(rèn)事務(wù)等級(jí),保證了一個(gè)事務(wù)不會(huì)讀到另一個(gè)并行事務(wù)已修改但未提交的數(shù)據(jù),避免了“臟讀取”。該級(jí)別適用于大多數(shù)系統(tǒng)。
4、Read Uncommitted:保證了讀取過程中不會(huì)讀取到非法數(shù)據(jù)。
spring中的Isolation屬性:
1、ISOLATION_DEFAULT :使用當(dāng)前數(shù)據(jù)源的默認(rèn)級(jí)別
2、ISOLATION_READ_UNCOMMITTED :Dirty reads, non-repeatable reads, and phantom reads can occur.
3、ISOLATION_READ_COMMITTED

4、ISOLATION_REPEATABLE_READ:Dirty reads and non-repeatable reads are prevented; phantom reads can occur.
5、ISOLATION_SERIALIZABLE:Dirty reads, non-repeatable reads, and phantom reads are prevented.
三、readOnly
事務(wù)屬性中的readOnly標(biāo)志表示對(duì)應(yīng)的事務(wù)應(yīng)該被最優(yōu)化為只讀事務(wù)。這是一個(gè)最優(yōu)化提示。在一些情況下,一些事務(wù)策略能夠起到顯著的最優(yōu)化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)時(shí)避免dirty checking(試圖“刷新”)。
四、Timeout
????? 在事務(wù)屬性中還有定義“timeout”值的選項(xiàng),指定事務(wù)超時(shí)為幾秒。在JTA中,這將被簡(jiǎn)單地傳遞到J2EE服務(wù)器的事務(wù)協(xié)調(diào)程序,并據(jù)此得到相應(yīng)的解釋。
事務(wù)劃分策略
1、推薦在業(yè)務(wù)層使用事務(wù),這樣可以允許業(yè)務(wù)層捕獲導(dǎo)致rollback的異常,并拋出恰當(dāng)?shù)臉I(yè)務(wù)層異常;不在dao層使用事務(wù)是因?yàn)檫@會(huì)限制了dao重用其他事務(wù)需求,并且dao層沒有實(shí)現(xiàn)業(yè)務(wù)邏輯,并且原子性也是業(yè)務(wù)層的概念。
spring聲明性事務(wù)的劃分:
1、有四個(gè)地方需要配置:The four participants are transaction manager, proxy factory, transaction interceptor, and a set of transaction attributes.
2、使用ProxyFactoryBean/Transaction Interceptor(transactionInterceptor)配置spring事務(wù)
以下為配置實(shí)例:
<!-- The DBCP DataSource -->
? <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"?
??????? destroy-method="close">
??? <property name="driverClassName">
????? <value>${jdbc.driverClassName}</value>
??? </property>
??? <property name="url"><value>${jdbc.url}</value></property>
??? <property name="username"><value>${jdbc.username}</value></property>
??? <property name="password"><value>${jdbc.password}</value></property>
? </bean>
??
? <!-- The DAO class -->
? <bean id="dao"
?? class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- The transactionmanager to use for regular non JTA datasource -->
? <bean id="transactionManager"????? class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- TransactionInterceptor -->
? <bean id="transactionInterceptor"??????? class="org.springframework.transaction.interceptor.TransactionInterceptor">??? <property name="transactionManager">????? <ref bean="transactionManager"/>??? </property>??? <property name="transactionAttributeSource">????? <value>org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,readOnlyorg.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIRED
????? </value>
??? </property>
? </bean>?
??
? <!-- Transactional proxy for the primary business object -->
? <bean id="boxOffice"??????????? class="org.springframework.aop.framework.ProxyFactoryBean">??? <property name="target">????? <ref local="boxOfficeTarget"/>??? </property>??? <property name="proxyInterfaces">????? <value>org.springframework.prospring.ticket.service.BoxOffice</value>??? </property>??? <property name="interceptorNames">????? <value>transactionInterceptor</value>??? </property>
? </bean>?
??
? <!-- Business Object -->
? <bean id="boxOfficeTarget"
??? class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
??? <property name="boxOfficeDao">
????? <ref local="dao"/>
??? </property>
? </bean>
3、使用TransactionProxyFactoryBean配置spring事務(wù)
以下為配置實(shí)例:
? <!-- The DBCP DataSource -->
? <bean id="dataSource"???????? class="org.apache.commons.dbcp.BasicDataSource"
??????? destroy-method="close">
??? <property name="driverClassName">
????? <value>${jdbc.driverClassName}</value>
??? </property>
??? <property name="url"><value>${jdbc.url}</value></property>
??? <property name="username"><value>${jdbc.username}</value></property>
??? <property name="password"><value>${jdbc.password}</value></property>
? </bean>
??
? <!-- The DAO class -->
? <bean id="dao"
class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- The transactionmanager to use for regular non JTA datasource -->
? <bean id="transactionManager"??? class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- Transactional proxy and the primary business object -->
? <bean id="boxOffice"?????? class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">??? <property name="transactionManager"><ref bean="transactionManager"/></property>??? <property name="target">????? <bean class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
??????? <property name="boxOfficeDao">
????????? <ref local="dao"/>
??????? </property>
????? </bean>
??? </property>
??? <property name="transactionAttributes">??????????? <props>???????????????? <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>???????????????? <prop key="allocate*">PROPAGATION_REQUIRED</prop>??????????? </props>?????? </property>
? </bean>?
4、使用BeanNameAutoProxyCreator配置spring事務(wù)
如果有大量的bean需要使用事物,那么只要在配置文件中提供bean name給BeanNameAutoProxyCreator,spring就會(huì)個(gè)給該bean提供事務(wù)代理,配置實(shí)例如下:
? <!-- The DBCP DataSource -->
? <bean id="dataSource"??? class="org.apache.commons.dbcp.BasicDataSource"
????? destroy-method="close">
??? <property name="driverClassName">
????? <value>${jdbc.driverClassName}</value>
??? </property>
??? <property name="url"><value>${jdbc.url}</value></property>
??? <property name="username"><value>${jdbc.username}</value></property>
??? <property name="password"><value>${jdbc.password}</value></property>
? </bean>
??
? <!-- The DAO class -->
? <bean id="dao"
?? class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- The transactionmanager to use for regular non JTA datasource -->
? <bean id="transactionManager"?????? class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
??? <property name="dataSource">
????? <ref local="dataSource"/>
??? </property>
? </bean>
??
? <!-- TransactionInterceptor -->
? <bean id="transactionInterceptor"???????? class="org.springframework.transaction.interceptor.TransactionInterceptor">??? <property name="transactionManager">????? <ref bean="transactionManager"/>??? </property>??? <property name="transactionAttributeSource">????? <value>org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,readOnlyorg.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIRED????? </value>??? </property>
? </bean>?
??
? <!-- BeanNameAutoProxyCreator -->
<bean id="autoProxyCreator"??? class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">? <property name="interceptorNames">??? <value>transactionInterceptor</value>? </property>? <property name="beanNames">??? <list>????? <idref local="boxOffice"/>??? </list>? </property>
</bean>?
??
<!-- Business Object -->
<bean id="boxOffice"
?? class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
? <property name="boxOfficeDao">
??? <ref local="dao"/>
? </property>
</bean>
http://www.blogjava.net/280211429/articles/75529.html
-----------------------------------------------------
Spring AOP特性的聲明式事務(wù)管理,就可以避免編程式事務(wù)帶來的難維護(hù)性。聲明式事務(wù)只需要在配置文件中聲明即可。這樣的好處是業(yè)務(wù)邏輯(Dao)就不會(huì)意識(shí)到事務(wù)管理的存在,而且維護(hù)起來極其方便。
簡(jiǎn)化配置:
<bean id="TransactionProxyFactoryBean" lazy-init="true"?
??????? class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">??
??????? <property name="transactionManager">??
??????????? <ref bean="transactionManager" />??
??????? </property>??
??????? <property name="transactionAttributes">??
??????????? <props>??
??????????????? <prop key="do*">PROPAGATION_REQUIRED</prop>??
??????????????? <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>??
??????????? </props>??
??????? </property>??
??? </bean>??
?
<bean id="ForumService" parent="TransactionProxyFactoryBean">??
?????????? <property name="target">???
??????????? <bean class="ForumServiceImp">???
??????? </property>???
</bean>??
<bean id="ForumServiceImp" class="com.bbs.service.imp.ForumServiceImp">??
??????? <property name="forumDAO">??
??????????? <ref local="ForumDAO" />??
??????? </property>??
??? </bean>?
-------------------------------------------------------
<!-- spring中配置 聲明式事務(wù)的方法 --> <!-- Transactional proxy and the primary business object --> <bean id="daytestLogic" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <!-- 代理的目標(biāo)類 --> <property name="target"> <bean class="com.xiaonei.wap.app.daytest.logic.impl.DaytestLogicImpl"> <property name="daytestDAO" ref="daytestDAO" /> <property name="optionDAO" ref="optionDAO" /> <property name="questionDAO" ref="questionDAO" /> <property name="answerDAO" ref="answerDAO" /> <property name="globalCacheManager" ref="globalCacheManager" /> </bean> </property> <!-- DaytestLogicImpl是類,實(shí)現(xiàn)了接口 --> <property name="proxyTargetClass" value="true" /> <property name="transactionManager" ref="daytestTransactionManager"/> <!-- 定義了事物的執(zhí)行策略,如傳播屬性,隔離等級(jí)等 --> <property name="transactionAttributes"> <props> <!-- PROPAGATION_REQUIRED支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就新建一個(gè)事務(wù) --> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <!-- 事務(wù)屬性中的readOnly標(biāo)志表示對(duì)應(yīng)的事務(wù)應(yīng)該被最優(yōu)化為只讀事務(wù) --> <prop key="*">PROPAGATION_REQUIRED, readOnly</prop> </props> </property> </bean>
TransactionProxyFactoryBean里的transactionAttributes,他的prop元素中的key屬性定義了target類的某個(gè)方法,可以使用通配符,如save*,prop元素的值定義了對(duì)key中定義方法應(yīng)用的事務(wù)執(zhí)行屬性,指定格式如下:
傳播行為,隔離等級(jí),只讀,+異常,-異常
其中傳播行為必須定義,其他可省,例如:
......
???? <prop key="xxxx">PROPAGATION_REQUIRED,readOnly,-DataAccessException</prop>
......
"-"表示發(fā)生指定異常后立即回滾,"+"表示發(fā)生指定異常后立即提交。
經(jīng)過上面xml定義,就相當(dāng)于在UserDaoImpl的saveOrUpdate方法前后加入了事務(wù)處理的代碼。
定義了userDaoProxy后,我們?cè)谙胧褂胾serDaoImpl,就需要注入userDaoProxy的bean了,而不是userDaoImpl的bean
? <property name="userDao">
??? <ref bean="userDaoProxy"/>
?? </property>
當(dāng)不想使用事務(wù)管理時(shí),把userController注入的userDaoProxy改為注入userDaoImpl就行了。
proxyTargetClass需要配置的情況只有一種:
? 當(dāng)TestTram類有實(shí)現(xiàn)某個(gè)接口,而TestProxyClass類中配置的類對(duì)象是TestTram時(shí)(而不是TestTram實(shí)現(xiàn)的接口),這時(shí)候你需要配置proxyTargetClass=true
?
? 如果TestTram沒有實(shí)現(xiàn)某個(gè)接口,而TestProxyClass類中配置的類對(duì)象是TestTram,這個(gè)時(shí)候我們是不需要配置proxyTargetClass=true的.(使用cgilib來動(dòng)態(tài)代理)
? 如果TestTram實(shí)現(xiàn)某個(gè)接口, 而TestProxyClass類中配置的是TestTram實(shí)現(xiàn)的interface的話.那樣我既不需要配置proxyInterface,也不需要配置proxyTargetClass
?
為什么我們?cè)跊]有配置proxyInterface情況下,去配置proxyTargetClass.因?yàn)閟pring會(huì)去拿到當(dāng)前配置的target實(shí)現(xiàn)的所有接口,然后通過動(dòng)態(tài)代理出類.
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
