AOP
是 Spring
比较重要的功能,可以动态的在指定方法前后添加功能或描述,具有的功能还是很强大的,今天给大家写一篇如何使用全注解配置 SpringAop
第一步肯定是先创建项目,然后导入依赖啦:
关于这个Lombok这个依赖,大家可以自行去了解一下,这里不做过多介绍
然后在把我们需要的类创建好,并添加相应的注解
AopConfig.java
这个类主要是进行一些配置,原来我们都是在Xml文件里面进行配置,现在改由配置文件。 @Configuration
这个注解标识这个类为一个配置文件, @ComponentScan
这个注解标识要扫描的哪些包下的注解类, @EnableAspectJAutoProxy
开启基于注解的 Aop
模式,该注解和 Xml
中的 <aop:aspectj-autoproxy/>
标签功能类似。
package com.aop.config;
import com.aop.method.Calculate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @Author Yangccck
* @Date 2019/12/7 16:33
* @Version 1.0
*/
@Configuration
@ComponentScan(value = "com.aop")
@EnableAspectJAutoProxy
public class AopConfig {
}
复制代码
Calculate.java
这个类的主要进行日常的操作,需要使用 @Component
进行标识该类由Spring管理,也可以在我们的配置类中使用 @Bean
或 @Import
等其他方式注册该 Bean
.这里我在每个方法里面加了句输出,以便后续测试查看。
package com.aop.method;
import org.springframework.stereotype.Component;
/**
* @Author Yangccck
* @Date 2019/12/7 16:36
* @Version 1.0
*/
@Component
public class Calculate {
/**
* 加法运算
* @param a
* @param b
* @return
*/
public int add(int a , int b){
System.out.println("正在进行加法运算...");
return a+b;
}
/**
* 减法运算
* @param a
* @param b
* @return
*/
public int subtract(int a , int b){
System.out.println("正在进行减法运算...");
return a-b;
}
/**
* 乘法运算
* @param a
* @param b
* @return
*/
public double multiply(double a , double b){
System.out.println("正在进行乘法运算...");
return a*b;
}
/**
* 除法运算
* @param a
* @param b
* @return
*/
public double division(double a , double b){
System.out.println("正在进行除法运算...");
return a/b;
}
}
复制代码
LogAspectJ.java
该类就是我们的 Aop
增强方法,根据 @Before
, @After
, @AfterReturning
等注解用于区别是前置通知还是后置通知等, @Aspect
注解告诉 Spring
这是一个切面类。这里有个需要注意的点 JoinPoint
一定要放在参数列表的前面,不然会报错。
package com.aop.aspectJ;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
* @Author 22367
* @Date 2019/12/7 16:41
* @Version 1.0
*/
@Aspect
@Component
public class LogAspectJ {
/**
* 抽取公共的接入点
*/
@Pointcut("execution(public * com.aop.method.Calculate .*(..))")
public void pointcut(){}
/**
* 前置通知
* @param joinPoint
*/
@Before("pointcut()")
public void beforeMethod(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName()+"开始进行运算了...参数是{"+ Arrays.toString(joinPoint.getArgs())+"}");
}
/**
* 后置通知
* @param joinPoint
*/
@After("pointcut()")
public void afterMethod(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName()+"运算结束了....");
}
/**
* 返回通知
* @param joinPoint
* @param returning
*/
@AfterReturning(value = "pointcut()",returning = "returning")
public void afterReturn(JoinPoint joinPoint,Object returning){
System.out.println(joinPoint.getSignature().getName()+"运算结束了.....返回值是{"+returning+"}");
}
/**
* 异常增强
* @param joinPoint
* @param e
*/
@AfterThrowing(value = "pointcut()",throwing = "e")
public void afterException(JoinPoint joinPoint,Exception e){
System.out.println(joinPoint.getSignature().getName()+"运算过程出现异常了.....异常信息是{"+e.getMessage()+"}");
}
}
复制代码
最后来一张主要类的结构图
最后我们编写测试类进行测试:一个是带异常的,另一个是不带异常的。
Aop
就到结束了,大家对此篇文章有什么疑问的可以在底下留言哦!感谢大家阅读!