从简单来说Aop【动态管理】:指在运行期间将某段代码切入到指定方法的指定位置运行的编程
下面带大家来测试一下Aop的功能测试
1 导入SpringAop的依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.2.13.RELEASE</version> </dependency>
1.1创建业务逻辑类
package com.Aop; //业务逻辑类 public class MathCalculator { public int div(int i,int j){ System.out.println("div方法被调用"); return i/j; } }
1.2定义日志切面类 定义@Aspect定义为切面
package com.Aop; import org.aspectj.lang.annotation.*; @Aspect public class LogAspect { @Pointcut("execution(public int com.Aop.MathCalculator.*(..))") public void pointcut(){ } @Before("pointcut()") public void logStart(){ System.out.println("除法运行前"); } @After("pointcut()") public void logEnd(){ System.out.println("除法运行后"); } @AfterReturning("pointcut()") public void logReturn(){ System.out.println("除法正常返回"); } @AfterThrowing("pointcut()") public void logException(){ System.out.println("除法异常返回"); } }
@Before("pointcut()"):目标方法运行前执行
@After("pointcut()"):目标方法运行后执行
@AfterReturning("pointcut()"):目标方法正常返回后执行
@AfterThrowing("pointcut()"): 目标方法异常后执行
1.3 定义Config类,并添加@EnableAspectAutoProxy ,开启注解Aop模式
.
package com.Config; import com.Aop.LogAspect; import com.Aop.MathCalculator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; //切入到指定位置 AOP【动态代理】 @Configuration @EnableAspectJAutoProxy public class AopConfig { @Bean public MathCalculator calculator(){ return new MathCalculator(); } @Bean public LogAspect aspect(){ return new LogAspect(); } }
1.4 创建测试类
package com; import com.Aop.MathCalculator; import com.Config.AopConfig; import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class AopTest { @Test public void test() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AopConfig.class); MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class); mathCalculator.div(2,1); } }
1.5 运行后
方法正常运行后得到的结果
如果你想要得到拿到值,我们可以在方法中添加JoinPoint来获取方法名
package com.Aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; @Aspect public class LogAspect { @Pointcut("execution(public int com.Aop.MathCalculator.*(..))") public void pointcut(){ } @Before("pointcut()") public void logStart(JoinPoint joinPoint){ Object[] args = joinPoint.getArgs(); System.out.println(joinPoint.getSignature().getName()+"除法运行前"); } @After("pointcut()") public void logEnd(JoinPoint joinPoint){ System.out.println(joinPoint.getSignature().getName()+"除法运行后"); } @AfterReturning(value = "pointcut()",returning="result") public void logReturn(JoinPoint joinPoint,Object result){ System.out.println(joinPoint.getSignature().getName()+"除法正常返回..."+result); } @AfterThrowing(value = "pointcut()",throwing="exception") public void logException(JoinPoint joinPoint,Exception exception){ System.out.println(joinPoint.getSignature().getName()+"除法异常返回"+exception); } }
在返回值中添加参数result,可以获得返回的值
运行后得到方法名和除法后的值