目录
- aop事务管理<aop:advisor>两种配置方式
- 方式一
- 方式二
- hibernate事务配置aop aop:advisor模式
aop事务管理<aop:advisor>两种配置方式
方式一
@transactionmanagerbean.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/xmlschema-instance" xmlns:context="https://www.springframework.org/schema/context" xmlns:aop="https://www.springframework.org/schema/aop" xmlns:tx="https://www.springframework.org/schema/tx" xsi:schemalocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd https://www.springframework.org/schema/tx https://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="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/xmlschema-instance" xmlns:context="https://www.springframework.org/schema/context" xmlns:aop="https://www.springframework.org/schema/aop" xmlns:tx="https://www.springframework.org/schema/tx" xsi:schemalocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd https://www.springframework.org/schema/tx https://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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。
需要了解更多c/c++开发分享AOP之事务管理<aop:advisor>的两种配置方式,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/992917.html