本文介绍SpringBoot中使用Spring AOP。
image
简介AOP简介AOP可能对于广大开发者耳熟能详,它是Aspect Oriented Programming的缩写,翻译成中文就是:面向切面编程。这个可能是面试中经常提到的问题,同时它也是Spring框架中一个重大的特性,AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果,对于我们开发中最常见的可能就是日志记录,事务处理,异常处理等等。
SpringBoot中使用AOP接下来介绍SpringBoot项目中如何运用AOP。
新建项目新建一个SpringBoot项目,在pom文件中加入AOP依赖,完整代码如下:
创建切面一 直接使用切面4.0.0 com.dalaoyang springboot_aop0.0.1-SNAPSHOT jar springboot_aop springboot_aop org.springframework.boot spring-boot-starter-parent2.0.4.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-aoporg.springframework.boot spring-boot-devtoolsruntime org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-maven-plugin
新建一个日志切面类,假设我们需要一个类来打印进入方法或方法执行后需要打印的日志。
新建一个切面类新建类LogAspect,完整代码如下:
package com.dalaoyang.aspect;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.springframework.stereotype.Component;import org.aspectj.lang.annotation.*;@Aspect@Componentpublic class LogAspect { @Pointcut("execution(public * com.dalaoyang.controller.*.*(..))") public void LogAspect(){} @Before("LogAspect()") public void doBefore(JoinPoint joinPoint){ System.out.println("doBefore"); } @After("LogAspect()") public void doAfter(JoinPoint joinPoint){ System.out.println("doAfter"); } @AfterReturning("LogAspect()") public void doAfterReturning(JoinPoint joinPoint){ System.out.println("doAfterReturning"); } @AfterThrowing("LogAspect()") public void deAfterThrowing(JoinPoint joinPoint){ System.out.println("deAfterThrowing"); } @Around("LogAspect()") public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("deAround"); return joinPoint.proceed(); } }
其中:
@Aspect 表明是一个切面类
@Component 将当前类注入到Spring容器内
@Pointcut 切入点,其中execution用于使用切面的连接点。使用方法:execution(方法修饰符(可选) 返回类型 方法名 参数 异常模式(可选)) ,可以使用通配符匹配字符,*可以匹配任意字符。
@Before 在方法前执行
@After 在方法后执行
@AfterReturning 在方法执行后返回一个结果后执行
@AfterThrowing 在方法执行过程中抛出异常的时候执行
@Around 环绕通知,就是可以在执行前后都使用,这个方法参数必须为ProceedingJoinPoint,proceed()方法就是被切面的方法,上面四个方法可以使用JoinPoint,JoinPoint包含了类名,被切面的方法名,参数等信息。
新建自定义注解,新建注解与新建接口类似,将interface改为@interface即可。
package com.dalaoyang.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface DoneTime { String param() default ""; }创建自定义注解对应切面
创建自定义注解对应切面,与上一中情况的切面类似,这里就不在赘述了,代码如下:
package com.dalaoyang.aspect;import com.dalaoyang.annotation.DoneTime;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component;import java.util.Date;@Aspect@Componentpublic class DoneTimeAspect { @Around("@annotation(doneTime)") public Object around(ProceedingJoinPoint joinPoint, DoneTime doneTime) throws Throwable { System.out.println("方法开始时间是:"+new Date()); Object o = joinPoint.proceed(); System.out.println("方法结束时间是:"+new Date()) ; return o; } }创建Controller测试
创建一个IndexController进行测试,其实就是两个普通的Web请求方法,其中index使用了自定义注解,index2没有使用自定义注解,完整代码如下:
package com.dalaoyang.controller;import com.dalaoyang.annotation.DoneTime;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController; @RestControllerpublic class IndexController { @GetMapping("/index") @DoneTime(param = "IndexController") public String index(){ System.out.println("方法执行"); return "hello dalaoyang"; } @GetMapping("/index2") public String index2(){ System.out.println("方法2执行"); return "hello dalaoyang"; } }运行测试
启动项目,在浏览器访问http://localhost:8080/index,控制台如下:
image
在浏览器访问http://localhost:8080/index2,控制台如下:
作者:dalaoyang
链接:https://www.jianshu.com/p/5afaeebb0a06