本博客demo源码地址
https://github.com/suchahaerkang/spring-annotation.git
1 AOP作用
AOP是spring中最终要的特性之一,底层技术用到了动态代理
AOP作用:在运行的程序中,将一段代码切入到指定方法指定位置上去运行的编程方法
2 AOP功能测试
要实现AOP的功能,首先要引入spring-aspects依赖包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
现在有个场景,我需要一个计算器的组件,里面有加减乘除的方法。我们现在需要在每个方法执行之前,执行之后,返回之后和抛出异常时都打印日志。如果按照每个方法都去写一遍这样逻辑,这是一个非常笨重的工作,做起来毫无意义。下面我们就用AOP的原理来实现这个需求。
首先写个计算器的组件MathCalculate
/**
* @description:
* @author: sukang
* @date: 2020-03-09 8:52
*/
public class MathCalculate {
/**
* @description: 计算除法的方法
* @param a
* @param b
* @return: int
* @author: sukang
* @date: 2020/3/9 8:52
*/
public int div(int a, int b){
System.out.println("div()方法执行...");
return a/b;
}
}
然后写个切面类
**
* @description: 日志切面
* @author: sukang
* @date: 2020-03-09 8:53
*/
//这个注解必须要写,不然不知道这个组件是个切面组件
@Aspect
public class LogAspects {
//切入点,标志切入的目标方法为MathCalculate组件里面的所有方法
@Pointcut("execution(public int com.wolfx.aop.MathCalculate.*(..))")
public void pointCut(){
}
//前置通知:在指定方法执行之前进行执行这个方法,JoinPoint组件可以获取当前目标函数的一些信息
@Before("pointCut()")
public void logStart(JoinPoint joinPoint){
//获取参数列表
Object[] objects = joinPoint.getArgs();
System.out.println("方法"+ joinPoint.getSignature().getName() + "执行前执行切面的前置通知@Before打印请求参数为:"+ Arrays.toString(objects));
}
//后置通知:在指定方法执行之后执行这个方法
@After("pointCut()")
public void logEnd(JoinPoint joinPoint){
System.out.println("方法"+ joinPoint.getSignature().getName() + "执行后执行切面的后置通知");
}
/*
* 返回通知:是在目标方法返回的时候进行执行,logReturn()方法的表示结果的参数值result
* 要和注解@AfterReturning中returning的值一样,不然接受不了。另外JoinPoint这个组件作为
* logReturn方法的参数必须要放在第一个参数,否则会报错
*/
@AfterReturning(pointcut = "pointCut()", returning = "result")
public void logReturn(JoinPoint joinPoint, Object result){
System.out.println("方法"+ joinPoint.getSignature().getName() + "返回的时候执行切面的返回通知返回的结果为:" + result);
}
/*
* 异常通知:是在目标方法抛出异常的时候进行执行,logException()方法的表示异常的参数值exception
* 要和注解@AfterThrowing中exception的值一样,不然接受不了。另外JoinPoint这个组件作为
* logException方法的参数必须要放在第一个参数,否则会报错
*/
@AfterThrowing(pointcut = "pointCut()", throwing = "exception")
public void logException(JoinPoint joinPoint, Exception exception){
System.out.println("方法"+ joinPoint.getSignature().getName() + "抛出异常的时候执行切面的异常通知抛出异常信息为:" + exception);
}
}
写个配置类,将计算器组件MathCalculate组件和LogAspects组件注册到容器中去,并且开启切面自动代理注解@EnableAspectJAutoProxy
/**
* @description:
* @author: sukang
* @date: 2020-03-09 8:50
*/
@EnableAspectJAutoProxy
@Configuration
public class MainConfigOfAop {
@Bean
public MathCalculate mathCalculate(){
return new MathCalculate();
}
@Bean
public LogAspects logAspects(){
return new LogAspects();
}
}
写个测试用例
//测试AOP功能
@Test
public void test01(){
//创建容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAop.class);
//从容器中获取MathCalculate的对象
MathCalculate mathCalculate = (MathCalculate) applicationContext.getBean("mathCalculate");
//调用mathCalculate的div方法
mathCalculate.div(4,2);
}
运行结果
如果发生异常,写个测试用例
//测试AOP功能
@Test
public void test01(){
//创建容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAop.class);
//从容器中获取MathCalculate的对象
MathCalculate mathCalculate = (MathCalculate) applicationContext.getBean("mathCalculate");
//调用mathCalculate的div方法
mathCalculate.div(4,0);
}
运行结果
3 总结
使用spring注解驱动的切面编程必须一下几步:
1) 切面组件和目标组件必须要注册到容器中去
2) 切面组件必须用注解@Aspects标注,不然不知道这个组件为切面类
3) 配置类上面必须用@EnableAspectJAutoProxy开启aop的注解驱动,不然的话不会生效
4)调用目标方法时,必须要从容器中中获取对象,自己用new创建的对象调用目标方法,也没有切面通知的效果