一、AOP
Spring架构有两个特点,一个是IoC,一个是AOP,IoC(Inversion of Control)控制反转,将JavaBean对象交给容器管理,解除了对象之间的依赖。
而AOP(Aspect oriented programming)面向切面编程,Spring架构的Controller、Service和DAO层可以看作一条条业务线,而切面就像一把刀贯穿到这些业务线中:
不改变原有代码,添加一些通用的辅助逻辑,侵入性低,可扩展性高。
通常,使用AOP的业务场景有:日志打印、数据权限、安全校验、错误处理等
二、实现
(一)添加依赖
在build.gradle中添加以下依赖
implementation(
'org.springframework.boot:spring-boot-starter-aop'
)
(二)数据库和对应的实体类准备
CREATE TABLE `sys_log` (
`id` varchar(50) COLLATE utf8mb4_bin NOT NULL COMMENT '主键',
`username` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '用户名',
`clazz` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '类名',
`method` varchar(500) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '方法',
`args` varchar(500) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '参数',
`exception` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
`create_date` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
本人只设计了用户名、类名、方法、参数、异常和创建时间这几个字段,需要的话,还可以添加ip,用户账号,执行时间等等。
然后写上对应的实体类,DAO层和Service层,这里就不赘述了
(三)自定义注解
1.@interface
注解的定义通过@interface表示,只能使用public修饰。
2.注解
@Target
:定义注解的作用目标:
ElementType.TYPE 类、接口、注解、enum
ElementType.FIELD 成员变量、对象、属性、枚举的常量
ElementType.METHOD 方法
ElementType.PARAMETER 参数
ElementType.CONSTRUCTOR 构造函数
ElementType.LOCAL_VARIABLE 局部变量
ElementType.ANNOTATION_TYPE 注解
ElementType.PACKAGE 包@RetentionPolicy
:定义注解的生命周期
RetentionPolicy.SOURCE : 仅存在于源代码中,编译阶段会被丢弃,不会包含于class字节码文件中
RetentionPolicy.CLASS : 默认策略,在class字节码文件中存在,在类加载的时被丢弃,运行时无法获取到。
RetentionPolicy.RUNTIME : 始终不会丢弃,可以使用反射获得该注解的信息。自定义的注解最常用的使用方式。@Documented
是否添加到javadoc@Inherited
子类是否继承这个注解
最后那两个我一般不怎么使用
/**
* 自定义注解@Log
*
* @author caicai
* @create 2021/6/9
*/
@Target({ElementType.TYPE, ElementType.METHOD}) // 作用于类、接口、注释及枚举, 方法
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
public @interface Log {
String value() default "";
}
(四)切面
/**
* @author caicai
* @create 2021/6/9
*/
@Aspect
@Component
@Slf4j
public class SysLogAspect {
@Autowired
private SysLogService sysLogService;
@Pointcut("@annotation(com.caicai.emipe.aop.Log)")
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
Object obj = null;
String exception = null;
try {
obj = point.proceed();
} catch (Exception e) {
if (e instanceof ControllerException) {
ControllerException controllerException = (ControllerException) e;
exception = controllerException.getCode() + "," + controllerException.getMessage();
}
throw e;
} finally {
saveSysLog(point, exception);
}
return obj;
}
/**
* 操作记录储存到库
*
* @param point
* @param exception
*/
private void saveSysLog(ProceedingJoinPoint point, String exception) {
MethodSignature signature = (MethodSignature) point.getSignature();
// 获取类名
String clazzName = point.getTarget().getClass().getName();
// 获取方法名
String methodName = signature.getName();
//获取RequestAttributes
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//从获取RequestAttributes中获取HttpServletRequest的信息
HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
String userName = (String) request.getAttribute("userName");
// 获取请求参数名
LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
String[] params = discoverer.getParameterNames(signature.getMethod());
// 获取请求参数值
Object[] args = point.getArgs();
List<String> strList = new ArrayList<>();
for (int i = 0; i < params.length; i++) {
strList.add(params[i] + ": " + args[i].toString());
}
sysLogService.save(new SysLog(userName, clazzName, methodName, strList.toString(), exception));
}
}
1.类注解
@Aspect和**@Component**是切面必须使用的类注解
@Order(1) 标记定义了组件的加载顺序,值越小拥有越高的优先级,如果有需要可以添加,我这里没有添加
2.@Pointcut()切入点
// 切入点
// @PointCut的参数标记了切面的作用范围
@Pointcut("@annotation(com.caicai.emipe.aop.Log)")
// @Pointcut("execution(* com.caicai.emipe.controller.*.*(..))"),可以使用&&,||,!连接
public void pointcut() {
}
这里的@Pointcut的参数标记了切面的作用范围,比较常见的有execution和annotation,execution通过匹配来标记,annotation通过注解来标记,这次我使用的是注解的方式,其实在正式的工作中,日志记录这一场景更适合使用execution,将某个包的所有Controller都标记。数据权限相关的接口可以使用注解标记,因为不是所有接口都需要校验数据权限,用注解更加灵活。