Spring 一开始最强大的便是 IOC / AOP 两大核心功能,我们本日一起来学习一下 Spring AOP 常见表明和实行顺序。
Spring 口试 核心点:
IOC、AOP、Bean注入、Bean的生命周期、Bean的循环依赖

首先我们一起来回顾一下 Spring Aop 中常用的几个表明:
@Before 前置关照:目标方法之前实行@After 后置关照:目标方法之后实行(始终实行)@AfterReturning 返回之后关照:实行方法结束之前实行(非常不实行)@AfterThrowing 非常关照:出喷鼻香非常后实行@Around 环抱关照:环抱目标方法实行常见问题1、你肯定知道 Spring , 那说说 Aop 的去全部关照顺序, Spring Boot 或者 Spring Boot 2 对 aop 的实行顺序影响?
2、说说你在 AOP 中碰着的那些坑?
示例代码下面我们先快速构建一个 spring aop 的 demo 程序来一起谈论 spring aop 中的一些细节。
配置文件为了方便我直策应用 spring-boot 进行快速的项目搭建,大家可以利用 idea 的spring-boot 项目快速创建功能,或者去 start.spring.io 上面去快速创建spring-boot 运用。
由于本人常常手动去网上贴一些依赖导致,依赖冲突做事启动失落败等一些问题。
plugins { id 'org.springframework.boot' version '2.6.3' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java'}group 'io.zhengsh'version '1.0-SNAPSHOT'repositories { mavenCentral() maven { url 'https://repo.spring.io/milestone' } maven { url 'https://repo.spring.io/snapshot' }}dependencies { # 实在这里也可以不增加 web 配置,为了试验大略,大家请忽略 implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-aop' testImplementation 'org.springframework.boot:spring-boot-starter-test'}tasks.named('test') { useJUnitPlatform()}
接口类
首先我们须要定义一个接口。我们这里可以再来回顾一下 JDK 的默认代理实现的选择:
如果目标工具实现了接口,则默认采取JDK动态代理如果目标工具没有实现接口,则采取进行动态代理如果目标工具实现了接口,且逼迫Cglib,则利用cglib代理这块的逻辑在 DefaultAopProxyFactory 大家有兴趣可以去看看。
public interface CalcService { public int div(int x, int y);}
实现类
这里我门就大略一点做一个除法操作,可以仿照正常也可以很随意马虎的仿照缺点。
@Servicepublic class CalcServiceImpl implements CalcService { @Override public int div(int x, int y) { int result = x / y; System.out.println("====> CalcServiceImpl 被调用了,我们的打算结果是:" + result); return result; }}
aop 拦截器
申明一个拦截器我们要为当前工具增加 @Aspect 和 @Component ,笔者之前也是才踩过这样的坑,只加了一个。
实在这块我刚开始也不是很理解,但是我看了 Aspect 表明的定义我就清楚了
这里面根本就没有 Bean 的定义。以是我们还是乖乖的加上两个表明。
还有便是如果当测试的时候须要开启Aop 的支持为配置类上增加@EnableAspectJAutoProxy 表明。
实在 Aop 利用就三个步骤:
定义 Aspect 定义切面定义 Pointcut 便是定义我们切入点定义详细的关照,比如: @After, @Before 等。@Aspect@Componentpublic class MyAspect { @Pointcut("execution( io.zhengsh.spring.service.impl...(..))") public void divPointCut() { } @Before("divPointCut()") public void beforeNotify() { System.out.println("----===>> @Before 我是前置关照"); } @After("divPointCut") public void afterNotify() { System.out.println("----===>> @After 我是后置关照"); } @AfterReturning("divPointCut") public void afterReturningNotify() { System.out.println("----===>> @AfterReturning 我是前置关照"); } @AfterThrowing("divPointCut") public void afterThrowingNotify() { System.out.println("----===>> @AfterThrowing 我是非常关照"); } @Around("divPointCut") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { Object retVal; System.out.println("----===>> @Around 环抱关照之前 AAA"); retVal = proceedingJoinPoint.proceed(); System.out.println("----===>> @Around 环抱关照之后 BBB"); return retVal; }}
测试类
实在我这个测试类,虽然用了 @Test 表明,但是我这个类更加像一个 main 方法把:如下所示:
实行结论
结果记录:spring 4.x, spring-boot 1.5.9
无法现在依赖,以是无法试验
我直接说一下结论:Spring 4 中环抱关照是在最里面实行的
结果记录:spring 版本5.3.15 springboot 版本2.6.3
img
多切面的情形多个切面的情形下,可以通过@Order指定先后顺序,数字越小,优先级越高。如下图所示:
代理失落效场景
下面一种场景会导致 aop 代理失落效,由于我们在实行 a 方法的时候实在实质是实行 AServer#a 的方法拦截器(MethodInterceptor)链, 当我们在 a 方法内直接实行b(), 实在实质就相称于 this.b() , 这个时候由实行 a方法是调用到 a 的原始工具相称于是 this 调用,那么会导致 b() 方法的代理失落效。这个问题也是我们开拓者在开拓过程中最常碰着的一个问题。
@Servicepublic class AService { public void a() { System.out.println("...... a"); b(); } public void b() { System.out.println("...... b"); }}