文章目录
- Springboot-cli 开发脚手架系列
- 简介
- 1. 依赖
- 2. 定义注解
- 3. 定义切面
- 4. 效果演示
- 6. 源码分享
简介
-
AOP面向切面编程
是通过预编译方式和运行期动态代理,实现在不修改源代码的情况下给程序动态统一添加功能的一种技术,同时是对OOP(面向对象编程)的补充和完善,常被用来在spring中实现日志记录、性能监控等功能。
面向对象实现日志记录,性能监控这些功能时,需要在每个对象中都添加相同的方法,这样就产生了较大的重复工作量和大量的重复代码,不利于维护,使用AOP,可以大大减少代码数量,方便维护。 -
AOP实现原理
Spring 实现AOP思想使⽤的是动态代理技术
默认状况下, Spring会根据被代理对象是否实现接⼝来选择使⽤JDK仍是CGLIB。当被代理对象没有实现
任何接⼝时, Spring会选择CGLIB。当被代理对象实现了接⼝, Spring会选择JDK官⽅的代理技术,不过能够经过配置的⽅式,让Spring强制使⽤CGLIB -
面向切面编程的步骤
接下我们以实战案例讲解AOP的实现步骤
- pom.xml
org.springframework.boot
spring-boot-starter-aop
com.alibaba
fastjson
1.2.80
2. 定义注解
- 自定义一个注解CustomLog.java接口。
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD})
@Documented
public @interface CustomLog {
String value() default "";
}
- @Retention注解的生命周期
1、RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;
2、RetentionPolicy.CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;
3、RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在; - @Target:注解的作用目标
@Target(ElementType.TYPE)——接口、类、枚举、注解
@Target(ElementType.FIELD)——字段、枚举的常量
@Target(ElementType.METHOD)——方法
@Target(ElementType.PARAMETER)——方法参数
@Target(ElementType.CONSTRUCTOR) ——构造函数
@Target(ElementType.LOCAL_VARIABLE)——局部变量
@Target(ElementType.ANNOTATION_TYPE)——注解
@Target(ElementType.PACKAGE)——包
- 日记实体封装SysLog.java
@Data
@Accessors(chain = true)
public class SysLog implements Serializable {
private static final long serialVersionUID = -6309732882044872298L;
private Long logId;
private Long userId;
private String packageName;
private Long executionTime;
private String method;
private String params;
private String desc;
private Date createTime;
}
- 切面
@Aspect
@Component
@Slf4j
public class LogAspect {
@Pointcut("@annotation(com.springboot.cli.annotation.CustomLog)")
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
SysLog sysLog = this.getMethodInfo(joinPoint);
long start = System.currentTimeMillis();
Object proceed = joinPoint.proceed();
long end = System.currentTimeMillis();
sysLog.setCreateTime(new Date())
.setExecutionTime(end - start);
log.info(JSON.toJSONString(sysLog));
return proceed;
}
private SysLog getMethodInfo(ProceedingJoinPoint joinPoint) {
SysLog sysLog = new SysLog();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
CustomLog customLog = method.getAnnotation(CustomLog.class);
// 注解上的描述
Optional.ofNullable(customLog).ifPresent(c -> sysLog.setDesc(c.value()));
try {
sysLog
.setMethod(joinPoint.getSignature().getName())
.setPackageName(joinPoint.getTarget().getClass().getName())
.setParams(JSON.toJSONString(this.getParameters(joinPoint)));
} catch (Exception e) {
e.printStackTrace();
}
return sysLog;
}
private Map getParameters(ProceedingJoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
String[] parameterNames = methodSignature.getParameterNames();
Object[] parameterValues = joinPoint.getArgs();
Map paramsMap = new HashMap<>(2);
for (int i = 0; i < parameterValues.length; i++) {
try {
Object s = parameterValues[i];
paramsMap.put(parameterNames[i], s);
} catch (Exception e) {
e.printStackTrace();
}
}
return paramsMap;
}
private HttpServletRequest getRequest() {
return ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
}
}
-
@Aspect:作用是把当前类标识为一个切面供容器读取
-
@Pointcut:Pointcut是植入Advice的触发条件。每个Pointcut的定义包括2部分,一是表达式,二是方法签名。方法签名必须是 public及void型。可以将Pointcut中的方法看作是一个被Advice引用的助记符,因为表达式不直观,因此我们可以通过方法签名的方式为 此表达式命名。因此Pointcut中的方法只需要方法签名,而不需要在方法体内编写实际代码。
-
@Around:环绕增强,相当于MethodInterceptor
-
@AfterReturning:后置增强,相当于AfterReturningAdvice,方法正常退出时执行
-
@Before:标识一个前置增强方法,相当于BeforeAdvice的功能,相似功能的还有
-
@AfterThrowing:异常抛出增强,相当于ThrowsAdvice
-
@After: final增强,不管是抛出异常或者正常退出都会执行
- 编写测试接口IndexController.java
- 在需要生成日记的方法上添加@CustomLog注解
@RestController
public class IndexController {
@GetMapping("/test")
@CustomLog("基础web")
public String test(String msg) {
return "欢迎使用 springboot-cli !";
}
}
- 浏览器输入http://localhost:9999/test?msg=hello
- 可以看到添加@CustomLog注解后,日记就自动生成了,这种规则的日记,在实际开发中对于日记的收集和分析是很有帮助的。
- Springboot-cli开发脚手架,集合各种常用框架使用案例,完善的文档,致力于让开发者快速搭建基础环境并让应用跑起来。
- 项目源码github地址
- 项目源码国内gitee地址



