Spring之AOP通知顺序

Spring之AOP通知顺序,第1张

Spring之AOP通知顺序 一、面试题:

1、Aop 的全部通知顺序 以及Springboot 或 Springboot2 对 Aop 的执行顺序影响

2、说说你使用 Aop 中碰到的坑

二、Spring4,5与SpringBoot1,2的关联以及对通知的影响 一、前置代码准备

1、service层

接口:
public interface CalcService {
    public int div(int x, int y);
}

实现类:
@Service
public class CalcServiceImpl implements CalcService {
    @Override
    public int div(int x, int y) {
        int result = x / y;
        System.out.println("=========>CalcServiceImpl被调用了,我们的计算结果:" + result);
        return result;
    }
}

2、切面类

@Aspect
@Component
public class MyAspect {
    @Before("execution(public int com.heygo.spring.aop.CalcServiceImpl.*(..))")
    public void beforeNotify() {
        System.out.println("******** @Before我是前置通知MyAspect");
    }

    @After("execution(public int com.heygo.spring.aop.CalcServiceImpl.*(..))")
    public void afterNotify() {
        System.out.println("******** @After我是后置通知");
    }

    @AfterReturning("execution(public int com.heygo.spring.aop.CalcServiceImpl.*(..))")
    public void afterReturningNotify() {
        System.out.println("********@AfterReturning我是返回后通知");
    }

    @AfterThrowing("execution(public int com.heygo.spring.aop.CalcServiceImpl.*(..))")
    public void afterThrowingNotify() {
        System.out.println("********@AfterThrowing我是异常通知");
    }

    @Around("execution(public int com.heygo.spring.aop.CalcServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object retValue = null;
        System.out.println("我是环绕通知之前AAA");
        retValue = proceedingJoinPoint.proceed();
        System.out.println("我是环绕通知之后BBB");
        return retValue;
    }
}
二、Spring4与SpringBoot1对通知顺序的影响

Spring4对应的是SpringBoot1

1、加入依赖


        org.springframework.boot
        spring-boot-starter-parent
        
        1.5.9.RELEASE
        
    
    4.0.0

    com.heygo
    interview1024
    0.0.1-SNAPSHOT

    
        1.8
    

    
        
        
            ch.qos.logback
            logback-core
            1.1.3
        
        
            ch.qos.logback
            logback-access
            1.1.3
        
        
            ch.qos.logback
            logback-classic
            1.1.3
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        
        
            cn.hutool
            hutool-captcha
            4.6.8
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2、创建测试类

@SpringBootTest
@RunWith(SpringRunner.class)  //1.5.9
public class AopTest {
    @Autowired
    private CalcService calcService;

    @Test
    public void testAop4() {
        System.out.println("spring版本:" + SpringVersion.getVersion() + "t" + "SpringBoot版本:" + SpringBootVersion.getVersion());
        System.out.println();
        calcService.div(10, 2);
        // calcService.div(10, 0);
    }
}

3、结果

正常情况下

 异常情况下

三、Spring5与SpringBoot2对通知顺序的影响

1、pom文件引入

SpringBoot 2.3.3.RELEASE 版本的对应的 Spring 版本为 5.2.8 Release




    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.3.RELEASE
        
        
    
    4.0.0

    com.heygo
    interview1024
    0.0.1-SNAPSHOT

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        
        
            cn.hutool
            hutool-captcha
            4.6.8
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


二、结果

1、正常执行

 2、执行异常

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5722018.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存