日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

使用Spring AOP 自動代理配置聲明式事務

系統 1865 0

我們可以使用下面兩種方式消除proxyFactoryBean的繁重配置

1.繼承parent bean

2.使用aop?自動代理

spring中的事務都是基于AOP的,我們同樣可以使用自動代理消除TransactionProxyFactoryBean的重負實例

首先,我們要做任何自動通知一樣,需要聲明一個bean,成為DefaultAdvisorAutoProxyCreator

?

< bean? id ="org.springfranework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" >
</ bean >

?DefaultAdvisorAutoProxyCreator將在應用上下文中遍歷advisor,自動用它們來代理匹配advisor的pointcut的所有bean,對于事務,應該使用advisor是TransactionAttributeSourceAdvisor

?

< bean? id ="transactionAdvisor" ?class ="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor" >
????
< constructor-arg >
??????
< ref? bean ="transactionInterceptor" />
????
</ constructor-arg >
?
</ bean >

?

TransactionAttributeSourceAdvisor是一個羽翼豐滿的AOP Advisor,同樣,他有一個pointcut和一個advisor構成,這個pointcut是靜態方法pointcut,他根據事物屬性來源確定一個方法是否和一些事務屬性關聯,關于攔截器advisor,通過一個構造參數transactionInterceptor

???

< bean? id ="TransactionManager" ?class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
?????????
< property? name ="dataSource" >
?????????????
< ref? bean ="dataSource" />
?????????
</ property >
</ bean > ?
< bean? id ="transactionInterceptor" ?class ="org.springframework.transaction.interceptor.TransactionInterceptor" >
??
< property? name ="transactionManager" >
????
< ref? bean ="transactionManager" /> ?
??
</ property >
??
< property? name ="transactionAttributeSource" >
????
< ref? bean ="transactionAttributeSource" />
??
</ property >
</ bean >

?

為自動代理選擇一個transactionAttributeSource

?

< bean? id ="transactionAttributeSource" ?class ="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource" >
???
< property? name ="properties" >
?????
< props >
???????
< prop? key ="get*" > PROPAGATION_SUPPORTS </ prop >
?????
</ props >
???
</ property >
</ bean >

?

關于屬性源,我們有一個更好的解決方法

?


< bean? id ="transactionAttributeSource" ?class ="org.springframework.transaction.interceptor.MethodMapTransactionAttributeSource" >
???
< property? name ="methodMap" >
???? <map>
?????????????? <entry key="com.springaction.service.CourseServiceImpl.get*">
???????????????????? <value>PROPAGATION_SUPPORT</value>
??????????????? </entry>
???????? </map>
???? ? </ property >
</ bean >

?

等等還沒有完,我們更好的選擇是使用AttributesTransactionAttributeSouce,即元數據,我們使用方法事務化和非事務化,只不過是添加合適的元數據到方法里而已



使用Spring AOP 自動代理配置聲明式事務


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 遵义县| 涟水县| 八宿县| 祁门县| 卢氏县| 沿河| 崇州市| 义乌市| 壶关县| 公安县| 永康市| 英吉沙县| 三河市| 浪卡子县| 林甸县| 青铜峡市| 什邡市| 开鲁县| 北辰区| 拉萨市| 新沂市| 永年县| 青田县| 龙游县| 钦州市| 三亚市| 定安县| 安龙县| 靖宇县| 六盘水市| 兰坪| 罗平县| 上杭县| 沅陵县| 凤山县| 江山市| 民县| 柘荣县| 海淀区| 阳新县| 元谋县|