AOP之事务管理<aop:advisor>的两种配置方式
作者:Simon_liu94 发布时间:2023-11-24 22:55:06
标签:AOP,事务管理,aop:advisor,配置
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


猜你喜欢
- 隐藏标题栏基于xml<application android:theme="@style/Them
- 项目背景因为公司需要对音视频做一些操作,比如说对系统用户的发音和背景视频进行合成,以及对多个音视频之间进行合成,还有就是在指定的源背景音频中
- /** * Name: 求数组中元素重复次数对多的数和重复次数 * Description: * 数组中的元
- 之前我们有介绍通过Spring Boot Admin来检测服务的上下线,然后进行通知功能。https://www.jb51.net/arti
- 应用情景:很多标准的方法都是利用Object.Equals方法来做对比的,例如LIst.Remove假设 某些情景下我们希望引用类型判断&a
- 一.static关键字的用途在《Java编程思想》P86页有这样一段话:“static方法就是没有this的方法。在st
- 本文实例讲述了Android编程获取网络连接方式及判断手机卡所属运营商的方法。分享给大家供大家参考,具体如下:问题:项目中写的网络模块,感觉
- 1. 前言Spring提供了xml、注解、JavaConfig多种方式来配置bean,不论何种方式,Spring最终都会将bean封装成Be
- 23种设计模式第七篇:java代理模式定义:为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不适合或者不能直接引用另一个
- 今天一起学习下如何在Spring中进行异步编程。我们都知道,web服务器处理请求 request 的线程是从线程池中获取的,这也不难解释,因
- 详解android 通过uri获取bitmap图片并压缩很多人在调用图库选择图片时会在onActivityResult中用Media.get
- 本文实例讲述了Android6.0开发中屏幕旋转原理与流程。分享给大家供大家参考,具体如下:从Android 系统开发开始,这里写下Andr
- 本教程源码https://github.com/bestaone/HiAuth源码比较全面,教程我就只介绍关键代码了,喜欢的点个star,谢
- Java动态数组Arraylist存放自定义数据类型class Point{ int x; int y; public Point(int
- 生命太短暂,不要去做一些根本没有人想要的东西。本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术
- 本文实例讲述了Java基于分治法实现的快速排序算法。分享给大家供大家参考,具体如下:package cn.nwsuaf.quick;/**
- 为什么需要在应用程序中增加渠道信息?Android应用的发布需要面对各种各样的市场,我们称之为渠道。有的时候,我们需要知道应用是从哪个渠道下
- 昨天写了一个关于Excel文件处理的脚本,在字符串匹配功能上总是出现多余不正确的匹配,debug调试之后,发现一个坑。------->
- 代码MyCalculator.h#pragma once#include <QtWidgets/QMainWindow>#inc
- 先看一下java线程运行时各个阶段的运行状态线程是进程中的一个实体,是被系 * 立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运