当前位置: 首页 > news >正文

Spring-AOP事务

Spring-AOP&事务

1.AOP简介

1.1概念

AOP,Aspect Oriented Programming,面向切面编程,是对面向对象编程OOP的升华。OOP是纵向对一个事物的抽象,一个对象包括静态的属性信息,包括动态的方法信息等。而AOP是横向的对不同事物的抽象,属性与属性、方法与方法、对象与对象都可以组成一个切面,而用这种思维去设计编程的方式叫做面向切面编程

1.2实现方案

image-20250405153435622

1.3相关概念

image-20250405153526869

2.AOP的相关配置

<!--配置目标类,内部的方法是连接点-->
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>
<!--配置通知类,内部的方法是增强方法-->
<bean id=“myAdvice" class="com.itheima.advice.MyAdvice"/>

<aop:config>
    <!--配置切点表达式,对哪些方法进行增强-->
    <aop:pointcut id="myPointcut" expression="execution(void com.itheima.service.impl.UserServiceImpl.show1())"/>
    <!--切面=切点+通知-->
    <aop:aspect ref="myAdvice">
        <!--指定前置通知方法是beforeAdvice-->
        <aop:before method="beforeAdvice" pointcut-ref="myPointcut"/>
        <!--指定后置通知方法是afterAdvice-->
        <aop:after-returning method="afterAdvice" pointcut-ref="myPointcut"/>
    </aop:aspect>
</aop:config>

如果使用注解进行配置,使用@Component让目标类和通知类进入Spring容器。在通知类上添加@Aspect

注解@Aspect、@Around需要被Spring解析,所以在Spring核心配置文件中需要配置aspectj的自动代理

<aop:aspectj-autoproxy/>

如果使用配置类,在配置类上添加注解@EnableAspectJAutoProxy

2.1切点的配置方式

切点表达式的配置方式有两种,直接将切点表达式配置在通知上,也可以将切点表达式抽取到外面,在通知上进行引用

<aop:config>
    <!--配置切点表达式,对哪些方法进行增强-->
    <aop:pointcut id="myPointcut" expression="execution(void 
    com.itheima.service.impl.UserServiceImpl.show1())"/>
    <!--切面=切点+通知-->
    <aop:aspect ref="myAdvice">
        <!--指定前置通知方法是beforeAdvice-->
        <aop:before method="beforeAdvice" pointcut-ref="myPointcut"/>
        <!--指定后置通知方法是afterAdvice-->
        <aop:after-returning method="afterAdvice" pointcut="execution(void 
        com.itheima.service.impl.UserServiceImpl.show1())"/>
    </aop:aspect>
</aop:config>

2.2切点表达式

//表示访问修饰符为public、无返回值、在com.itheima.aop包下的TargetImpl类的无参方法show
execution(public void com.itheima.aop.TargetImpl.show())
//表述com.itheima.aop包下的TargetImpl类的任意方法
execution(* com.itheima.aop.TargetImpl.*(..))
//表示com.itheima.aop包下的任意类的任意方法
execution(* com.itheima.aop.*.*(..))
//表示com.itheima.aop包及其子包下的任意类的任意方法
execution(* com.itheima.aop..*.*(..))
//表示任意包中的任意类的任意方法
execution(* *..*.*(..))

抽取切点表达式

//切点表达式抽取
@Pointcut("execution(* com.itheima.aop.*.*(..))")
public void pointcut(){}
//前置通知
@Before("pointcut()")
public void before(JoinPoint joinPoint){}

2.3通知的类型

image-20250405153921579

各种注解方法通知类型

//前置通知
@Before("execution(* com.itheima.aop.*.*(..))")
public void before(JoinPoint joinPoint){}
//后置通知
@AfterReturning("execution(* com.itheima.aop.*.*(..))")
public void AfterReturning(JoinPoint joinPoint){}
//环绕通知
@Around("execution(* com.itheima.aop.*.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {}
//异常通知
@AfterThrowing("execution(* com.itheima.aop.*.*(..))")
public void AfterThrowing(JoinPoint joinPoint){}
//最终通知
@After("execution(* com.itheima.aop.*.*(..))")
public void After(JoinPoint joinPoint){}

通知方法在被调用时,Spring可以为其传递一些必要的参数

image-20250405153946420

2.4配置方式

通过aop:aspect标签进行配置

还可以通过advisor标签进行配置

AOP的另一种配置方式,该方式需要通知类实现Advice的子功能接口

public interface Advice {}

image-20250405154133569

通知类实现了方法拦截器接口

public class MyMethodInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("前置逻辑功能...");
        //执行目标方法
        Object invoke = 
        methodInvocation.getMethod().invoke(methodInvocation.getThis(),methodInvocation.getArguments());
        System.out.println("后置逻辑功能...");
        return invoke;}}

切面使用advisor标签配置

<aop:config>
    <!-- 将通知和切点进行结合 -->
    <aop:advisor advice-ref=“myMethodInterceptor" pointcut="execution(void 
    com.itheima.aop.TargetImpl.show())"/>
</aop:config>

3.原理剖析

通过xml方式配置AOP时,我们引入了AOP的命名空间,根据讲解的,要去找spring-aop包下的META-INF,在去找spring.handlers文件

最终加载的是 AopNamespaceHandler,该Handler的init方法中注册了config标签对应的解析器

以ConfigBeanDefinitionParser作为入口进行源码剖析,最终会注册一个AspectJAwareAdvisorAutoProxyCreator进入到Spring容器中

image-20250405154508970

这个类实际上实现了BeanPostProcessor后处理器的接口

最后会调用AopProxy类中的getProxy的方法,返回一个代理对象。这个方法有两种实现

image-20250405154723058

image-20250405154729304

image-20250405155329516

4.基于AOP的声明式事务控制

4.1事务控制的分类

事务是开发中必不可少的东西,使用JDBC开发时,我们使用connnection对事务进行控制,使用MyBatis时,我们使用SqlSession对事务进行控制,缺点显而易见,当我们切换数据库访问技术时,事务控制的方式总会变化,Spring 就将这些技术基础上,提供了统一的控制事务的接口。Spring的事务分为:编程式事务控制 和 声明式事务控制

image-20250405155435300

4.2事务控制的配置

结合上面我们学习的AOP的技术,很容易就可以想到,可以使用AOP对Service的方法进行事务的增强。

  • 目标类:AccountServiceImpl
  • 切点:service业务类中的所有业务方法
  • 通知类:Spring提供的,通知方法已经定义好,只需要配置即可
<!--Spring提供的事务通知-->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    	<tx:method name="transferMoney"/>
    </tx:attributes>
</tx:advice>

<!--平台事务管理器-->
<bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<aop:config>
    <aop:advisor advice-ref="myAdvice" pointcut="execution(* 
    com.itheima.service.impl.*.*(..))"/>
</aop:config>

平台事务管理器PlatformTransactionManager是Spring提供的封装事务具体操作的规范接口,封装了事务的提交和回滚方法

public interface PlatformTransactionManager extends TransactionManager {
    TransactionStatus getTransaction(@Nullable TransactionDefinition var1) throws 
    TransactionException;
    void commit(TransactionStatus var1) throws TransactionException;
    void rollback(TransactionStatus var1) throws TransactionException;
}

不同的持久层框架事务操作的方式有可能不同,所以不同的持久层框架有可能会有不同的平台事务管理器实现,例如,MyBatis作为持久层框架时,使用的平台事务管理器实现是DataSourceTransactionManager。Hibernate作为持久层框架时,使用的平台事务管理器是HibernateTransactionManager。

使用注解进行配置

@Service("accountService")
public class AccoutServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    //<tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED“/>
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = 
    Propagation.REQUIRED,readOnly = false,timeout = 5)
    public void transferMoney(String decrAccountName, String incrAccountName, int money) {
        accountMapper.decrMoney(decrAccountName,money); //转出钱
        int i = 1/0; //模拟某些逻辑产生的异常
        accountMapper.incrMoney(incrAccountName,money); //转入钱}}

需要在xml文件中开启事务注解

<bean id="transactionManager" 
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务的注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager"/>

如果要使用全注解,需要配置类@EnableTransactionManagement,同时内部构造Bean并返回

4.3事务特性

其次,事务定义信息配置,每个事务有很多特性,例如:隔离级别、只读状态、超时时间等,这些信息在开发时可以通过connection进行指定,而此处要通过配置文件进行配置

<tx:attributes>
    <tx:method name="方法名称"
                isolation="隔离级别"
                propagation="传播行为"
                read-only="只读状态"
                timeout="超时时间"/>
</tx:attributes>

image-20250405155739352

(1)isolation属性

指定事务的隔离级别,事务并发存在三大问题:脏读、不可重复读、幻读/虚读。可以通过设置事务的隔离级别来保证并发问题的出现,常用的是READ_COMMITTED 和 REPEATABLE_READ

image-20250405160545042

(2)read-only属性

设置当前的只读状态,如果是查询则设置为true,可以提高查询性能,如果是更新(增删改)操作则设置为false

(3)timeout属性

设置事务执行的超时时间,单位是秒,如果超过该时间限制但事务还没有完成,则自动回滚事务,不在继续执行。默认值是-1,即没有超时时间限制

(4)propagation属性

设置事务的传播行为,主要解决是A方法调用B方法时,事务的传播方式问题的,例如:使用单方的事务,还是A和B都使用自己的事务等。事务的传播行为有如下七种属性值可配置

image-20250405160650483

4.4原理

使用了AOP的动态代理技术,通过advisor标签进行配置,有一个通知类实现Advice的子功能接口MethodInterceptor,从而实现环绕通知(方法执行之前开启事务,执行之后提交事务)

相关文章:

  • 按钮特效网站网页制作教程
  • 个人建网站教程开发网站用什么软件
  • 在网站上做教学直播平台多少钱热门搜索
  • ip做网站域名官网建设
  • 免费html网页模板素材网站活动营销案例100例
  • 网站之间的区别网站排名推广
  • [ctfshow web入门] web4
  • PDF转换:在线将PDF转PPT并且可编辑文字图片,超快速转换软件,无需下载
  • 通过Postman和OAuth 2.0连接Dynamics 365 Online的详细步骤
  • 区块链日记5 - Solana入门 - 部署第一个Solana程序
  • 23种设计模式-行为型模式-命令
  • ubuntu22使用TrinityCore搭建魔兽世界服务器
  • 【计算机网络】Linux配置SNAT/DNAT策略
  • G-Retriever: 用于文本图理解与问答的检索增强生成
  • LeetCode算法题(Go语言实现)_31
  • 基于LSTM的文本分类2——文本数据处理
  • 数据流和重定向
  • 250405-VSCode编辑launch.json实现Debug调试Open-WebUI
  • 数据库原理
  • [题解]2025HDU春季联合(五) - 小凯逛超市
  • JAX、Flax 和 PyTorch 之间的类比关系
  • 【doris】在线事务处理
  • Chapter07_图像压缩编码
  • 苍穹外卖Day2
  • 文件操作(C语言)
  • 蓝桥云客---蓝桥速算