本文最后更新于:2022年5月28日 凌晨
摘要:
AOP 功能测试
AOP:【动态代理】——指在程序运行期间动态地将某段代码切入到指定方法指定位置运行的编程方式
- 通知方法:
- 前置通知 @Before
- 后置通知 @After
- 返回通知 @AfterReturning
- 异常通知 @AfterThrowing
- 环绕通知 @Around
- 导入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.20</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.20</version> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>compile</scope> </dependency> </dependencies>
|
- 定义一个业务逻辑类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package io.github.shgang.math;
import org.springframework.stereotype.Component;
@Component public class MathCalculator {
public int div(int i, int j) { System.out.println("MathCalculator.div"); return i / j; }
}
|
- 定义一个记录日志的切面类,加上通知注解
- @Aspect:声明当前类为切面类
- 可以在通知方法的的参数中加上JoinPoint
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package io.github.shgang.aspect;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;
@Aspect @Component public class LogAspects {
@Pointcut("execution(public int io.github.shgang.math.MathCalculator.*(..))") public void pointCut() {
}
@Before("pointCut()") public void logStart(JoinPoint joinPoint) { Object[] args = joinPoint.getArgs(); String arguments = ""; for (Object arg : args) { arguments += (arg + "\t"); } System.out.printf("方法运行开始,参数列表是:{%s}\n", arguments); }
@After("pointCut()") public void logEnd() { System.out.println("方法运行结束"); }
@AfterReturning("pointCut()") public void logReturn() { System.out.println("方法正常返回,运行结果是:{}"); }
@AfterThrowing("pointCut()") public void logException() { System.out.println("方法出现异常"); }
}
|
- 创建一个配置类
- @EnableAspectJAutoProxy:开启基于注解的aop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package io.github.shgang.config;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration @ComponentScan(value = {"io.github.shgang.math", "io.github.shgang.aspect"}) @EnableAspectJAutoProxy public class AopConfig {
}
|
- 运行查看结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package io.github.shgang.test;
import io.github.shgang.config.AopConfig; import io.github.shgang.math.MathCalculator; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test01 {
@Test public void test01() { ApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class); MathCalculator mathCalculator = context.getBean(MathCalculator.class); System.out.println(mathCalculator.div(10, 5)); } }
|
1 2 3 4 5
| 方法运行开始,参数列表是:{10 5 } MathCalculator.div 方法正常返回,运行结果是:{} 方法运行结束 2
|