當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring-----AOP-----事务
生活随笔
收集整理的這篇文章主要介紹了
Spring-----AOP-----事务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ?xml文件中:
?
手動處理事務:
設置數據源
<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>
? ? ? 創建事務管理器,因為使用的是jdbc操作數據庫,所以使用DataSourceTransactionManager事務管理需要事務,事務來自Connection,即來自連接池,所以要
? ? ?注入數據源。
? ?
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property>
</bean>
創建事務模板,事務模板需要事務,事務在管理器中,所以需要注入事務管理器 <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManager"></property>
</bean>
接下來就是具體的實現類來注入事務模板
<!--創建service對象,并注入dao,注入事務-->
<bean id="accountServiceId" class="com.luo.transfer2.dao.serviceImpl.AccountServiceImpl">
<property name="accountDao" ref="accountDaoId"></property>
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>
Spring半自動管理事務(使用SpringFactoryBean創建代理對象)
使用Spring的半自動方式生成代理對象,Service就是目標類,事務就是切面類 <bean id="proxyBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
代理對象要實現的接口 <property name="proxyInterfaces" value="com.luo.transfer3.service.AccountService"></property>
目標類 <property name="target" ref="accountServiceId"></property>
切面類 <property name="transactionManager" ref="txManager"></property>
//注入事務的一些屬性 <property name="transactionAttributes">
<!--key是service中的方法名,
PROPAGATION ISOLATION -EXCEPTION +EXCEPTION
發生異常時回滾 發生異常時也不回滾
-->
<props>
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
</props>
</property>
-------------------------------------------------------------------------------------------------
全自動方式 使用Spring的aop創建代理對象
<tx:advice transaction-manager="txManager" id="aspectId">
<!--配置事務的屬性-->
<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>
<!--創建事務管理器,因為使用的是jdbc操作數據庫,所以使用DataSourceTransactionManager
事務管理需要事務,事務來自Connection,即來自連接池,所以要注入數據源
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
? ? ? ? ? ? ?
轉載于:https://www.cnblogs.com/Leroyo/p/8360270.html
總結
以上是生活随笔為你收集整理的Spring-----AOP-----事务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到自己的手多长了几根手指头
- 下一篇: 【一针见血】 JavaScript t