當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring-----AOP-----事务
生活随笔
收集整理的這篇文章主要介紹了
Spring-----AOP-----事务
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ?xml文件中:
?
手動(dòng)處理事務(wù):
設(shè)置數(shù)據(jù)源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/transfer"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
? ? ? 創(chuàng)建事務(wù)管理器,因?yàn)槭褂玫氖莏dbc操作數(shù)據(jù)庫,所以使用DataSourceTransactionManager事務(wù)管理需要事務(wù),事務(wù)來自Connection,即來自連接池,所以要
? ? ?注入數(shù)據(jù)源。
? ?
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property>
</bean>
創(chuàng)建事務(wù)模板,事務(wù)模板需要事務(wù),事務(wù)在管理器中,所以需要注入事務(wù)管理器 <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManager"></property>
</bean>
接下來就是具體的實(shí)現(xiàn)類來注入事務(wù)模板
<!--創(chuàng)建service對象,并注入dao,注入事務(wù)-->
<bean id="accountServiceId" class="com.luo.transfer2.dao.serviceImpl.AccountServiceImpl">
<property name="accountDao" ref="accountDaoId"></property>
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>
Spring半自動(dòng)管理事務(wù)(使用SpringFactoryBean創(chuàng)建代理對象)
使用Spring的半自動(dòng)方式生成代理對象,Service就是目標(biāo)類,事務(wù)就是切面類 <bean id="proxyBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
代理對象要實(shí)現(xiàn)的接口 <property name="proxyInterfaces" value="com.luo.transfer3.service.AccountService"></property>
目標(biāo)類 <property name="target" ref="accountServiceId"></property>
切面類 <property name="transactionManager" ref="txManager"></property>
//注入事務(wù)的一些屬性 <property name="transactionAttributes">
<!--key是service中的方法名,
PROPAGATION ISOLATION -EXCEPTION +EXCEPTION
發(fā)生異常時(shí)回滾 發(fā)生異常時(shí)也不回滾
-->
<props>
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
</props>
</property>
-------------------------------------------------------------------------------------------------
全自動(dòng)方式 使用Spring的aop創(chuàng)建代理對象
<tx:advice transaction-manager="txManager" id="aspectId">
<!--配置事務(wù)的屬性-->
<tx:attributes>
<tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="aspectId" pointcut="execution(* com.luo.transfer4.serviceImpl..*.*(..))"></aop:advisor>
</aop:config>
<!--創(chuàng)建事務(wù)管理器,因?yàn)槭褂玫氖莏dbc操作數(shù)據(jù)庫,所以使用DataSourceTransactionManager
事務(wù)管理需要事務(wù),事務(wù)來自Connection,即來自連接池,所以要注入數(shù)據(jù)源
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
? ? ? ? ? ? ?
轉(zhuǎn)載于:https://www.cnblogs.com/Leroyo/p/8360270.html
總結(jié)
以上是生活随笔為你收集整理的Spring-----AOP-----事务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到自己的手多长了几根手指头
- 下一篇: 梦到男友离开是有什么预兆吗