Spring AOP 总结

1.简介

Spring AOP是Spring针对面向切面编程的落地方案,用于解决非核心代码的冗余问题。

Spring AOP只能运用于存于IoC容器中的对象。

2.知识

2.1 底层实现

Spring AOP框架针对不同情况有两种底层实现基于Jdk的动态代理和基于cglib:

  • 动态代理:用于有接口的类,采用继承接口的方式实现代理。
  • cglib:用于没有接口的类,采用继承子类的方式实现代理。

2.2 插入点

Spring AOP框架总共有5种插入点:

  • 前置通知@Before: 目标函数执行前执行
  • 返回通知@AfterReturning:目标函数执行后执行
  • 异常通知@AfterThrowing:catch中执行
  • 后置通知@After:finally中执行
  • 环绕通知@Around:包含上面所有通知节点,完全自定义

3.代码

要使用Spring AOP,必须加入以下组件到pom.xml中

xml 复制代码
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>6.0.6</version>
</dependency>

配置类中开启AOP

java 复制代码
@Configuration//声明这是一个配置类
@ComponentScan("com.jojo")//设置扫描范围
@EnableAspectJAutoProxy//开启AOP
public class JavaConfig {
}

切面增强类的编写:

java 复制代码
@Component//增强类必须放入IoC容器
@Aspect//声明切面
@Order(2)//设置优先级,值越小,优先级越高,前置先执行,后置后执行
public class LogAdvice {
    @Before("com.jojo.pointcut.MyPointCut.point()")
    public void before(JoinPoint joinPoint){//目标方法的封装对象
        //1.获取的类名
        String simpleName = joinPoint.getTarget().getClass().getSimpleName();
        //2.获取方法名称
        int modifiers = joinPoint.getSignature().getModifiers();
        String s = Modifier.toString(modifiers);//获取是public还是其他
        String name = joinPoint.getSignature().getName();//获取方法名
        //3.获取参数列表
        Object[] args = joinPoint.getArgs();//获取参数
    }

    @AfterReturning(value = "com.jojo.pointcut.MyPointCut.point()",returning = "result")//接受核心函数返回变量到result
    public void afterReturning(JoinPoint joinPoint, Object result){//设置返回变量

    }

    @After("com.jojo.pointcut.MyPointCut.point()")
    public void after(JoinPoint joinPoint){

    }

    @AfterThrowing(value = "execution(* com..impl.*.*(..))",throwing = "throwable")//接受异常信息到throwable
    public void afterThrowing(JoinPoint joinPoint,Throwable throwable){//设置异常信息变量

    }
    
    @Around("com.jojo.pointcut.MyPointCut.point()")//声明环绕通知
    public Object transaction(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("前置通知");//增强代码,前置通知
        Object[] args = joinPoint.getArgs();//目标参数获取
        System.out.println("返回通知");//增强代码,返回通知
        Object result = null;
        try {
            result = joinPoint.proceed(args);//目标函数调用
        } catch (Throwable e){
            System.out.println("异常通知");//增强代码,异常通知
            throw new RuntimeException(e);//抛出异常
        } finally{
        	System.out.println("后置通知");//增强代码,后置通知
        }
        return result;//目标方法的返回值
    }
}

插入点类的编写

java 复制代码
@Component
public class MyPointCut {
    @Pointcut("execution(* com.jojo.service.impl.*.*(..))")//插入点统一管理
    public void point(){}
}

测试:

java 复制代码
@SpringJUnitConfig(value = JavaConfig.class)//声明使用的配置类
public class SpringAopTest {
    @Autowired
    //针对有接口类必须使用接口而非接口实现类来声明
    //针对无接口类使用实现类来声明
    private MyTest myTest;


    @Test
    public void test(){
        int result = myTest.do(1);
        System.out.println("result = "+ result);
    }
}
相关推荐
猹叉叉(学习版)2 分钟前
【ASP.NET CORE】 12. DDD基本概念
笔记·后端·架构·c#·asp.net·.netcore
ZZhYasuo4 分钟前
冒泡排序1
java·算法·排序算法
独断万古他化5 分钟前
【抽奖系统开发实战】Spring Boot 项目的奖品模块开发:文件上传、时序设计与奖品创建
java·spring boot·后端·mvc·文件
东离与糖宝6 分钟前
告别AI投毒!Java后端实现大模型prompt过滤与敏感信息拦截实战
java·人工智能
饕餮争锋7 分钟前
Supabase 简介
后端·开源
yashuk7 分钟前
怎么下载安装yarn
java
23.8 分钟前
【Java】Arrays工具类——数组操作终极指南
java·算法·面试
ok_hahaha8 分钟前
java从头开始-苍穹外卖-day12-数据统计以及excel报表
java
好家伙VCC8 分钟前
# Deno实战:从零搭建一个安全、现代的后端服务在Node.js生态逐渐臃肿
java·python·安全·node.js
littlegirll9 分钟前
一个KADB报错分析及实验
java·javascript·数据库