AOP之事务管理的两种配置方式
程序员文章站
2022-03-15 19:30:31
目录aop事务管理两种配置方式方式一方式二hibernate事务配置aop aop:advisor模式aop事务管理两种配置...
aop事务管理<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--> <!--事务管理器--> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"/> </bean> <tx:annotation-driven /> </beans>
serviceimpl
在service实现类的最外层或者具体方法上添加@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 事务管理配置基于xml--> <!--事务管理器--> <bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"/> </bean> <!--事务隔离级别--> <tx:advice id="txadvice" transaction-manager="txmanager"> <tx:attributes> <!--还可以添加回滚、只读等标签配置--> <tx:method name="*" /> </tx:attributes> </tx:advice> <!--业务层 使用 事务切面--> <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事务配置aop aop:advisor模式
<!-- 使用hibernatetransactionmanager管理hibernate事务 --> <bean id="txmanager" class="org.springframework.orm.hibernate3.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory"></property> </bean> <!-- 创建事务规则 --> <!-- 表示我们要控制事务的地方,如果方法名开头是add、update和delete,那么使用required事务传播方式。那么其他的方法使用required事务传播方式,并且是只读 --> <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> <!-- 告知事务的切入点 --> <aop:config> <aop:advisor advice-ref="txadvice" pointcut="execution(* com.tiema..service..*.*(..))" /> </aop:config>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。