聊聊aop执行顺序

简介: 聊聊aop执行顺序

@Before   前置通知    目标方法之前执行

@After  - 后置通知   目标方法之后执行

@AfterReturning   返回后通知:执行方法结束前通知(异常不执行)

@AfterThrowing   异常通知 :出现异常时候执行

@Around    环绕通知:环绕目标方法执行


测试示例代码

package com.roy.aop;
public interface DivideService {
    int div(int x, int y);
}
package com.roy.aop;
import org.springframework.stereotype.Service;
@Service
public class DivideServiceImpl implements DivideService {
    @Override
    public int div(int x, int y) {
        int result = x / y;
        System.out.println("  DivideServiceImpl 被调用,测试结果为  >>>>" + result);
        return result;
    }
}
package com.roy.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAopAsepct {
    @Before("execution(public int com.roy.aop.DivideServiceImpl.*(..))")
    public void beforeNotify() {
        System.out.println(" ******************@Before   我是前置通知  MyAopAsepct");
    }
    @After("execution(public int com.roy.aop.DivideServiceImpl.*(..))")
    public void afterNotify() {
        System.out.println(" ******************@After  我是后置通知 ");
    }
    @AfterReturning("execution(public int com.roy.aop.DivideServiceImpl.*(..))")
    public void afterReturningNotify() {
        System.out.println(" ******************@AfterReturning  我是返回后通知 ");
    }
    @AfterThrowing("execution(public int com.roy.aop.DivideServiceImpl.*(..))")
    public void afterThrowingNotify() {
        System.out.println(" ******************@AfterThrowing  我是异常通知 ");
    }
    @Around("execution(public int com.roy.aop.DivideServiceImpl.*(..))")
    public Object aroundNotify(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object rev = null;
        System.out.println("我是环绕通知之前 AAA ");
        rev = proceedingJoinPoint.proceed();
        System.out.println("我是环绕通知之后 BBB ");
        return rev;
    }
}
package com.roy.aop;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.SpringVersion;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestAop {
    @Autowired
    private DivideService divideService;
    @Test
    public void testAopAs() {
        System.out.println(" Spring 的版本 " + SpringVersion.getVersion() + "\t" + " >>> Spring Boot 版本" + SpringBootVersion.getVersion());
        divideService.div(10, 0);
    }
}



执行结果对比





目录
相关文章
|
Java 开发者 Spring
Spring Aop 常见注解和执行顺序(下)
Spring 一开始最强大的就是 IOC / AOP 两大核心功能,我们今天一起来学习一下 Spring AOP 常见注解和执行顺序。
272 0
Spring Aop 常见注解和执行顺序(下)
|
前端开发 Java 数据库
浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
|
Java API Spring
Spring Boot Aop 执行顺序
当我们在一个接口中使用多个 aop,时,就需要注意他们的执行顺序了。那么,它们的执行顺序是怎样的呢?如果不把这个问题搞明白,那我们的程序就不可控,这是不允许的,这就是我们今天要讨论的问题。
251 0
Spring Boot Aop 执行顺序
|
Java Spring
AOP切面执行顺序你真的了解吗
本文内容重点: 问题描述 Spring AOP执行顺序 探究顺序错误的真相 代码验证 结论
409 0
|
Java Spring
Spring Aop 常见注解和执行顺序(上)
Spring 一开始最强大的就是 IOC / AOP 两大核心功能,我们今天一起来学习一下 Spring AOP 常见注解和执行顺序。
530 0
Spring Aop 常见注解和执行顺序(上)
|
Java 开发者 Spring
Spring Aop 常见注解和执行顺序
Spring Aop 常见注解和执行顺序
Spring Aop 常见注解和执行顺序
|
26天前
|
XML 安全 Java
使用 Spring 的 @Aspect 和 @Pointcut 注解简化面向方面的编程 (AOP)
面向方面编程(AOP)通过分离横切关注点,如日志、安全和事务,提升代码模块化与可维护性。Spring 提供了对 AOP 的强大支持,核心注解 `@Aspect` 和 `@Pointcut` 使得定义切面与切入点变得简洁直观。`@Aspect` 标记切面类,集中处理通用逻辑;`@Pointcut` 则通过表达式定义通知的应用位置,提高代码可读性与复用性。二者结合,使开发者能清晰划分业务逻辑与辅助功能,简化维护并提升系统灵活性。Spring AOP 借助代理机制实现运行时织入,与 Spring 容器无缝集成,支持依赖注入与声明式配置,是构建清晰、高内聚应用的理想选择。
269 0
|
6月前
|
人工智能 监控 Java
面向切面编程(AOP)介绍--这是我见过最易理解的文章
这是我见过的最容易理解的文章,由浅入深介绍AOP面向切面编程,用科普版和专家版分别解说,有概念,有代码,有总结。
Micronaut AOP与代理机制:实现应用功能增强,无需侵入式编程的秘诀
AOP(面向切面编程)能够帮助我们在不修改现有代码的前提下,为应用程序添加新的功能或行为。Micronaut框架中的AOP模块通过动态代理机制实现了这一目标。AOP将横切关注点(如日志记录、事务管理等)从业务逻辑中分离出来,提高模块化程度。在Micronaut中,带有特定注解的类会在启动时生成代理对象,在运行时拦截方法调用并执行额外逻辑。例如,可以通过创建切面类并在目标类上添加注解来记录方法调用信息,从而在不侵入原有代码的情况下增强应用功能,提高代码的可维护性和可扩展性。
248 1
|
11月前
|
安全 Java 编译器
什么是AOP面向切面编程?怎么简单理解?
本文介绍了面向切面编程(AOP)的基本概念和原理,解释了如何通过分离横切关注点(如日志、事务管理等)来增强代码的模块化和可维护性。AOP的核心概念包括切面、连接点、切入点、通知和织入。文章还提供了一个使用Spring AOP的简单示例,展示了如何定义和应用切面。
1237 2
什么是AOP面向切面编程?怎么简单理解?