主要介绍springboot中aop的使用,用过Spring框架的都知道,aop是spring框架的两大核心功能之一,还有一个就是ioc,下面我们就springboot中如何引入aop来做一下探讨
因为在AOP的默认配置属性中,spring.aop.auto属性默认是开启的,也就是说只要引入了AOP依赖后,默认已经增加了@EnableAspectJAutoProxy
Springboot中有关AOP相关的自动配置包为:AopAutoConfiguration
上面所提到的五种通知方法中,除了环绕通知外,其他的四个通知注解中,加或者不加参数JoinPoint都可以,如果有用到JoinPoint的地方就加,用不到就可以不加。
JoinPoint:里包含了类名、被切面的方法名,参数等属性。
环绕通知:参数必须为ProceedingJoinPoint,pjp.proceed相应于执行被切面的方法。
返回通知:可以加returning = “XXX”,XXX即为被切入方法的返回值,本例中是controller类中方法的返回值。
异常通知:可以加throwing = “XXX”,供读取异常信息。
返回通知和异常通知只会执行一个,如果产生异常,返回通知就不执行,后置通知一定会执行
== 环绕通知一般单独使用,环绕通知可以替代上面的四种通知,后面单独介绍。==
注意:返回通知和异常通知只能有一个会被执行,因为发生异常执行异常通知,然后就不会继续向下执行,自然后置通知也就不会被执行,反之亦然。
使用aop来完成全局请求日志处理。
第一步:使用Spring Initializr快速创建一个springboot web项目,名称为testaop
第二步:引入aop相关的依赖
<!--aop相关的依赖引入--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
第三步:创建个controller
@RestController public class HelloController { @RequestMapping("/helloAop") public Object hello(){ return "hello aop"; } @RequestMapping("/helloError") public Object helloError(){ return 1/0; } }
第四步:创建一个aspect切面类
@Aspect @Component public class MyAop { //切入点:待增强的方法 @Pointcut("execution(public * com.aismall.testaop.controller.*.*(..))") //切入点签名 public void log(){ System.out.println("pointCut签名。。。"); } //前置通知 @Before("log()") public void deBefore(JoinPoint jp) throws Throwable { // 接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 记录下请求内容 System.out.println("URL : " + request.getRequestURL().toString()); System.out.println("HTTP_METHOD : " + request.getMethod()); System.out.println("CLASS_METHOD : " + jp); System.out.println("ARGS : " + Arrays.toString(jp.getArgs())); } //返回通知 @AfterReturning(returning = "ret", pointcut = "log()") public void doAfterReturning(Object ret) throws Throwable { // 处理完请求,返回内容 System.out.println("返回通知:方法的返回值 : " + ret); } //异常通知 @AfterThrowing(throwing = "ex", pointcut = "log()") public void throwss(JoinPoint jp,Exception ex){ System.out.println("异常通知:方法异常时执行....."); System.out.println("产生异常的方法:"+jp); System.out.println("异常种类:"+ex); } //后置通知 @After("log()") public void after(JoinPoint jp){ System.out.println("后置通知:最后且一定执行....."); } }
第六步:启动项目
URL : http://localhost:8080/helloAop HTTP_METHOD : GET CLASS_METHOD : execution(Object com.aismall.testaop.controller.HelloController.hello()) ARGS : [] 返回通知:方法的返回值 : hello aop 后置通知:最后且一定执行.....
URL : http://localhost:8080/helloError HTTP_METHOD : GET CLASS_METHOD : execution(Object com.aismall.testaop.controller.HelloController.helloError()) ARGS : [] 异常通知:方法异常时执行..... 产生异常的方法execution(Object com.aismall.testaop.controller.HelloController.helloError()) 异常种类java.lang.ArithmeticException: / by zero 后置通知:最后且一定执行.....
分析:返回通知和异常通知只会执行一个,后置通知一定会执行。
public void com.aismall.testaop.controller.HelloController.helloAop()
访问修饰符可以省略
void com.aismall.testaop.controller.HelloController.helloAop()
返回值可以使用通配符*,表示任意返回值
* com.aismall.testaop.controller.HelloController.helloAop()
包名可以使用通配符,表示任意包,但是有几级包,就需要写几个*.
* *.*.*.*.HelloController.helloAop()
包名可以使用…表示当前包及其子包
* *...HelloController.helloAop()
类名和方法名都可以使用*来实现通配
* *..*.*()
全通配写法:* ….*(…)
within(表达式):是用来指定类型的,指定类型中的所有方法将被拦截
com.aismall.testaop.controller.HelloController
举例:匹配HelloController类对应对象的所有方法,并且只能是HelloController的对象,不能是它的子对象。
within(com.aismall.testaop.controller.HelloController)
也可以使用通配符 * :匹配com.aismall包及其子包下面的所有类的所有方法。
within(com.aismall…*)
args() 匹配不带参数的方法
args(java.lang.String) 匹配方法参数是String类型的
==args(…) == 带任意参数的方法
args(java.lang.String,…) 匹配第一个参数是String类型的,其他参数任意。最后一个参数是String的同理
带有相应注解的方法,比如对标有@Transactional注解的方法进行增强
@annotation(org.springframework.transaction.annotation.Transactional)
参数带有相应标注的任意方法,比如@Transactional
@args(org.springframework.transaction.annotation.Transactional)
表达式可由多个切点函数通过逻辑运算组成
//PointCut表达式 @Pointcut("execution(public * com.aismall.testaop.controller.HelloController.*(..))") //PointCut签名 public void log(){ }
PointCut中的运算符:PointCut中可以使用 && 、|| 、! 运算
@Pointcut("execution(public * com.aismall.testaop.controller.HelloController.*(..))") public void cutController(){ } @Pointcut("execution(public * com.aismall.testaop.service.UserService.*(..))") public void cutService(){ } //使用 && 运算符,则cutAll()的作用等同于cutController 和 cutService 之和 @Pointcut("cutController() && cutService()") public void cutAll(){ }
@annotation方式
第一步:编写一个自定义注解
//表示次注解可以标注在类和方法上 @Target({ElementType.METHOD, ElementType.TYPE}) //运行时生效 @Retention(RetentionPolicy.RUNTIME) public @interface MyLogAnnotation { //定义一个变量,可以接受参数 String desc() default " "; }
第二步:在HelloController类中添加一个方法
@RequestMapping("helloAnnotation") //标有这个注解的方法会被增强 @MyLogAnnotation(desc = "@Annotation") public Object helloAnnotation() { return "hello annotation"; }
第三步:切面类
@Aspect @Component public class MyAopAnnotation { //切入点:增强标有MyLogAnnotation注解的方法 @Pointcut(value="@annotation(com.aismall.testaop.MyAnnotation.MyLogAnnotation)") //切入点签名 public void logAnnotation(){ System.out.println("pointCut签名。。。"); } //前置通知 @Before("logAnnotation()") public void deBefore(JoinPoint jp) throws Throwable { // 接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 记录下请求内容 System.out.println("URL : " + request.getRequestURL().toString()); } //返回通知 @AfterReturning(returning = "ret", pointcut = "logAnnotation()") public void doAfterReturning(Object ret) throws Throwable { // 处理完请求,返回内容 System.out.println("返回通知:方法的返回值 : " + ret); } //异常通知 @AfterThrowing(throwing = "ex", pointcut = "logAnnotation()") public void throwss(JoinPoint jp,Exception ex){ System.out.println("异常通知:方法异常时执行....."); System.out.println("产生异常的方法:"+jp); System.out.println("异常种类:"+ex); } //后置通知 @After("logAnnotation()") public void after(JoinPoint jp){ System.out.println("后置通知:最后且一定执行....."); } }
第四步:启动项目
URL : http://localhost:8080/helloAnnotation
返回通知:方法的返回值 : hello annotation
后置通知:最后且一定执行.....
spring的通知(Advice)中 一共有五种通知,之前已经介绍了四种,为什么不把环绕通知和它们放在一起说,因为环绕通知可以把前面的四种通知都表示出来,而且环绕通知一般单独使用
环绕通知的使用:
第一步:在HelloController类中添加一个方法
@RequestMapping("/helloAround") public Object helloAround(){ System.out.println("helloAround执行了。。。"); return "hello around"; }
第二步:切面类
@Aspect @Component public class MyAroundAop { //切入点:待增强的方法 @Pointcut("execution(public * com.aismall.testaop.controller.*.*(..))") //切入点签名 public void logAround() { System.out.println("pointCut签名。。。"); } //环绕通知,环绕增强,相当于MethodInterceptor @Around("logAround()") public Object aroundAdvice(ProceedingJoinPoint pjp) { Object rtValue = null; try { Object[] args = pjp.getArgs();//得到方法执行所需的参数 System.out.println("通知类中的aroundAdvice方法执行了。。前置"); rtValue = pjp.proceed(args);//明确调用切入点方法(切入点方法) System.out.println("通知类中的aroundAdvice方法执行了。。返回"); System.out.println("返回通知:"+rtValue); return rtValue; } catch (Throwable e) { System.out.println("通知类中的aroundAdvice方法执行了。。异常"); throw new RuntimeException(e); } finally { System.out.println("通知类中的aroundAdvice方法执行了。。后置"); } } }
第三步:启动项目
通知类中的aroundAdvice方法执行了。。前置
helloAround执行了。。。
通知类中的aroundAdvice方法执行了。。返回
返回通知:hello around
通知类中的aroundAdvice方法执行了。。后置