Spring AOP原理

AOP

在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率

  • 优势:减少重复代码,提高开发效率,维护方便
  • 实现方法:动态代理

Spring中AOP

XML配置

  1. 使用aop:config标签开始配置
  2. 使用aop:aspect标签表明配置切面
    属性:
    id给前面提供一个唯一标示
    ref:指定通知类bean的id
  3. aop:aspect内部配置通知的类型
前置/后置通知

例如:通过printlog方法在切入点(被代理类中的被代理方法)执行前执行,也就是前置通知
aop:before标示配置前置通知

aop:after-returning标示配置后置通知
aop:after-throwing标示配置异常通知
aop:after标示配置最终通知

属性:
method 用于指定代理类中哪个方法前置通知
printcut 用于指定切入点表达式,是指对业务层中那些方法进行增强

关键字:execution
切入点表达式:访问修饰符 返回值 包名.类名.方法名(参数列表)
全通配写法:* .*(…)

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">


<!--  sop  -->
<aop:config>
    <!--  配置切入点表达式  -->
    <aop:pointcut id="pc1" expression="execution(* demo.service.impl.*.*(..))"/>
    <!--  配置切面  -->
    <aop:aspect id="logAdvice" ref="logger">
        <!--  配置前置通知  -->
        <aop:before method="printLog" pointcut="execution(* *..*.*(..))"></aop:before>
        <!--  配置最终通知  -->
        <aop:after method="printLog" pointcut-ref="pc1"></aop:after>
    </aop:aspect>
</aop:config>
环绕通知

<aop:around>
Spring提供了一个接口,ProceedingJoinPoint该接口有一个方法proceed(),此方法相当于明确调用切入点方法。
将通过xml配置的方式,移入了对应通知方法中。

public Object aroundLog(ProceedingJoinPoint pjp){
    Object rtValue;
    try{
        Object[] args = pjp.getArgs();
        System.out.println("前置通知");
        rtValue = pjp.proceed(args);
        System.out.println("后置通知");
        return rtValue;
    }catch (Throwable t){
        System.out.println("异常通知");
        throw new RuntimeException(t);
    }finally {
        System.out.println("最终通知");
    }
}

注解AOP

@EnableAspectJAutoProxy // 开启自动代理AOP

加在config类上
在这里插入图片描述

@Aspect

在切面类上加注解,定义为一个切面类

@Component
@Aspect//当前类是一个切面类
public class Logger {}
@Pointcut

方法没有方法体,在其他注解调用时直接输入方法名即可

@Pointcut("execution(* demo.service.impl.*.*(..))")
    private void pc1(){}
@Before&@AfterReturning&@AfterThrowing@After
@Before("pc1()")
public void printLog(){
    System.out.println("前打印日志----------");
}
@AfterReturning("pc1()")
public void afterReturnPrintLog(){
    System.out.println("后置打印日志----------");
}
@AfterThrowing("pc1()")
public void afterThrowPrintLog(){
    System.out.println("异常打印日志----------");
}
@After("pc1()")
public void afterPrintLog(){
    System.out.println("结束打印日志----------");
}

但是这种方式可能会导致顺序出错,可以看出@After@AfterReturning之前执行

前打印日志----------
执行查找方法
结束打印日志----------
后置打印日志----------
Employee{id=10006, name='c'}
@Around

环绕打印不会出现结果顺序出错

@Around("pc1()")
public Object aroundLog(ProceedingJoinPoint pjp){
    Object rtValue;
    try{
        Object[] args = pjp.getArgs();
        System.out.println("前置通知");
        //明确调用业务层方法
        rtValue = pjp.proceed(args);

        System.out.println("后置通知");
        return rtValue;
    }catch (Throwable t){
        System.out.println("异常通知");
        throw new RuntimeException(t);
    }finally {
        System.out.println("最终通知");
    }
}
前置通知
执行查找方法
后置通知
最终通知
Employee{id=10006, name='c'}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值