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);
    }
}
相关推荐
随遇而安zx几秒前
【多线程】---AQS 原理 知识点(设计思想与源码深度解析)
java·多线程·aqs
LXMXHJ4 分钟前
springboot中的线程操作
java·spring boot·后端·线程
敲代码的嘎仔11 分钟前
自己设计了一个兑换码算法:自增ID + Base32转码 + 按位加权签名 + 异或混淆,面试被追问细节时终于不用慌了
java·数据库·mysql·算法·微服务·面试·职场和发展
Sinclair19 分钟前
MCP 功能详解:内置 108 个工具,让 AI 直接帮你管理网站
后端·mcp
用户2333768521823 分钟前
接口卡死排查实录-缺失return的UB死循环
前端·后端
用户4891487997642 分钟前
Go 系统服务开发实战:systemd+sdnotify + 看门狗-agent与系统服务
后端
拾光师43 分钟前
Python 模式匹配:从 in 到正则再到 match-case,一招对一招
后端
m0_587383001 小时前
外卖CPS系统开发实战:从架构设计到运营落地全指南
java·spring·小程序·架构·需求分析
zhangzeyuaaa1 小时前
Python asyncio 事件循环演进:从手动管理到现代化实践
java·服务器·python
只爱喝胡辣汤1 小时前
JUC 并发工具与线程安全源码深度解析
后端