AOP之事務(wù)管理<aop:advisor>的兩種配置方式
更新時間:2021年11月24日 08:51:04 作者:Simon_liu94
這篇文章主要介紹了AOP之事務(wù)管理<aop:advisor>的兩種配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
AOP事務(wù)管理<aop:advisor>兩種配置方式
方式一
@transactionManagerbean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.wanho.java150"/> <context:property-placeholder location="classpath*:jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--spring 提供 jdbc 幫助類--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!--基于@Transactional--> <!--事務(wù)管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven /> </beans>
ServiceImpl
在service實現(xiàn)類的最外層或者具體方法上添加@Transactional注解
@Service //@Transactional public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerDAO customerDAO ; @Autowired private LoginLogDAO loginLogDAO ; @Transactional @Override public Customer login(String account, String pswd) { return null; } }
方式二
bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.wanho.java150"/> <context:property-placeholder location="classpath*:jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--spring 提供 jdbc 幫助類--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!--jdbc 事務(wù)管理配置基于xml--> <!--事務(wù)管理器--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--事務(wù)隔離級別--> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!--還可以添加回滾、只讀等標簽配置--> <tx:method name="*" /> </tx:attributes> </tx:advice> <!--業(yè)務(wù)層 使用 事務(wù)切面--> <aop:config> <aop:pointcut id="servicePointcut" expression="execution(* com.wanho.java150.service.impl.CustomerServiceImpl.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/> </aop:config> </beans>
hibernate事務(wù)配置Aop aop:advisor模式
<!-- 使用HibernateTransactionManager管理hibernate事務(wù) --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 創(chuàng)建事務(wù)規(guī)則 --> <!-- 表示我們要控制事務(wù)的地方,如果方法名開頭是add、update和delete,那么使用REQUIRED事務(wù)傳播方式。那么其他的方法使用REQUIRED事務(wù)傳播方式,并且是只讀 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <!-- 告知事務(wù)的切入點 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.tiema..service..*.*(..))" /> </aop:config>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于JVM 調(diào)優(yōu)的技巧總結(jié)分析
本篇文章是對JVM 調(diào)優(yōu)的技巧進行了總結(jié)和分析。需要的朋友參考下2013-05-05Spring、SpringMVC和SpringBoot的區(qū)別及說明
這篇文章主要介紹了Spring、SpringMVC和SpringBoot的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。2022-10-10SpringBoot集成Swagger2實現(xiàn)Restful(類型轉(zhuǎn)換錯誤解決辦法)
這篇文章主要介紹了SpringBoot集成Swagger2實現(xiàn)Restful(類型轉(zhuǎn)換錯誤解決辦法),需要的朋友可以參考下2017-07-07Springboot項目對數(shù)據(jù)庫用戶名密碼實現(xiàn)加密過程解析
這篇文章主要介紹了Springboot項目對數(shù)據(jù)庫用戶名密碼實現(xiàn)加密過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06