AOP之事务管理<aop:advisor>的两种配置方式
作者:Simon_liu94 时间:2023-11-24 22:55:06
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>
来源:https://blog.csdn.net/m0_49552866/article/details/109430675
标签:AOP,事务管理,aop:advisor,配置
0
投稿
猜你喜欢
Java一个简单的红包生成算法
2023-12-12 10:56:50
SpringMVC结构简介及常用注解汇总
2023-10-25 09:16:59
使用idea创建web框架和配置struts的方法详解
2022-11-14 14:21:52
Spring3 整合MyBatis3 配置多数据源动态选择SqlSessionFactory详细教程
2023-03-21 21:22:27
JavaWeb Spring依赖注入深入学习
2022-04-02 08:20:05
Spring Cloud Ribbon的使用原理解析
2021-10-11 02:03:01
hibernate 命名查询如何实现
2023-11-04 02:35:41
Java Springboot整合支付宝接口的教程详解
2023-11-06 19:41:49
RabbitMQ交换机使用场景和消息可靠性总结分析
2023-10-06 14:00:55
Android开源AndroidSideMenu实现抽屉和侧滑菜单
2023-10-09 09:24:51
Android图片处理实例分析
2022-09-10 20:34:50
SpringBoot配置Email发送功能实例
2022-03-07 20:52:20
Java为什么基本数据类型不需要进行创建对象?
2022-03-16 08:59:03
Android高性能日志写入方案的实现
2022-09-18 17:12:50
C#类的访问修饰符用法分析
2021-12-22 22:23:01
android桌面悬浮窗显示录屏时间控制效果
2022-04-21 14:54:12
Android带清除功能的输入框控件EditTextWithDel
2022-02-13 16:53:27
Android TextView跑马灯效果实现方法
2023-09-27 04:16:09
Android仿QQ、微信聊天界面长按提示框效果
2023-06-11 14:04:32
Java常用类之System类的使用指南
2021-09-20 07:54:46