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

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

系統 1897 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元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 永春县| 泰兴市| 衡阳县| 松江区| 安多县| 永清县| 依安县| 马鞍山市| 普宁市| 鄢陵县| 巴彦县| 金华市| 原平市| 交口县| 大关县| 惠州市| 二连浩特市| 双峰县| 江源县| 会泽县| 淅川县| 普定县| 丰县| 竹山县| 伊宁市| 奉化市| 临沧市| 平山县| 西丰县| 利川市| 义马市| 夏邑县| 大余县| 远安县| 华安县| 荃湾区| 保康县| 长宁区| 沧源| 宣恩县| 横峰县|