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-parent1.5.9.RELEASE 4.0.0 com.heygo interview10240.0.1-SNAPSHOT 1.8 ch.qos.logback logback-core1.1.3 ch.qos.logback logback-access1.1.3 ch.qos.logback logback-classic1.1.3 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-jdbcorg.springframework.boot spring-boot-starter-aopcn.hutool hutool-captcha4.6.8 org.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-engineorg.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-parent2.3.3.RELEASE 4.0.0 com.heygo interview10240.0.1-SNAPSHOT 1.8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-jdbcorg.springframework.boot spring-boot-starter-aopcn.hutool hutool-captcha4.6.8 org.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-engineorg.springframework.boot spring-boot-maven-plugin
二、结果
1、正常执行
2、执行异常
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)