字段自动填充处理"/>
SpringAOP 对公共字段自动填充处理
为什么需要用AOP
需要在每一个业务方法中进行操作, 编码相对冗余、繁琐,那能不能对于这些公共字段在某个地方统一处理
举一个实例:
- 创建一个员工的时候,需要传入创建的时间
- 创建一个菜品的时候,也需要传入创建的时间
可能会有很多地方,都会多次需要传入创建的时间,如果我们每次都去set,也就是Employee.setCreateTime(LocalDateTime.now())
Category.setCreateTime(LocalDateTime.now())
那么代码就会相对冗余、繁琐。
AOP
之前大家也都学习过 OOP (Object Oriented Programming)面向对象编程;
那么 AOP(Aspect Oriented Programming)就是面向切面编程;
为什么选择AOP
在不惊动原始设计的基础上为其进行功能增强。简单的说就是在不改变方法源代码的基础上对方法进行功能增强。
如何实现
切入点是在哪里,如何找切入点;
controller调用service,通过mapper层写入的数据库,从而完成前端页面的展示的。那么我们就应该从mapper写入的时候,作为切入点,把时间设置进去。
如图:
SpringAOP中的JointPoint
JointPoint介绍
JointPoint是程序运行过程中可识别的点,这个点可以用来作为AOP切入点。JointPoint对象则包含了和切入相关的很多信息。比如切入点的对象,方法,属性等。我们可以通过反射的方式获取这些点的状态和信息,用于追踪tracing和记录logging应用信息。
JoinPoint使用
1.切入点的方法名字及其参数
2.切入点方法标注的注解对象(通过该对象可以获取注解信息)
3.切入点目标对象(可以通过反射获取对象的类名,属性和方法名)
注:有一点非常重要,Spring的AOP只能支持到方法级别的切入。换句话说,切入点只能是某个方法。
- 获取切入点所在目标对象
Object targetObj =joinPoint.getTarget();# 可以发挥反射的功能获取关于类的任何信息,例如获取类名如下String className = joinPoint.getTarget().getClass().getName();
- 获取切入点方法的名字
getSignature()
;是获取到这样的信息 :修饰符+ 包名+组件名(类名) +方法名
这里我只需要方法名
String methodName = joinPoint.getSignature().getName()
- 获取方法上的注解
方法1:xxxxxx是注解名字
Signature signature = joinPoint.getSignature();MethodSignature methodSignature = (MethodSignature) signature;Method method = methodSignature.getMethod();if (method != null){xxxxxx annoObj= method.getAnnotation(xxxxxx.class);}return null;
方法2:上面我们已经知道了方法名和类的对象,通过反射可以获取类的内部任何信息
// 切面所在类Object target = joinPoint.getTarget();String methodName = joinPoint.getSignature().getName();Method method = null;for (Method m : target.getClass().getMethods()) {if (m.getName().equals(methodName)) {method = m;// xxxxxx annoObj= method.getAnnotation(xxxxxx.class);同上break;}}
- 获取方法的参数
这里返回的是切入点方法的参数列表
Object[] args = joinPoint.getArgs();
项目实践
自定义注解类 AutoFill
其中 Operation Type 是一个枚举类。存放Update 和 Insert 两种数据库操作类型;
因为 我们是要在 数据库 修改和插入数据时,如果包含creatime 则自动填充;
/*** Description 自定义注解类 AutoFill* 自动填充 creattime updatetime* Create by salvatore* Date 2023/11/1 14:32*/@Target(ElementType.METHOD)// 该注解只能加载方法上
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {//数据库操作类型:UPDATE INSERTOperationType value();
}
Mapper 中添加 @AutoFill 注解
/*** 根据主键动态修改属性* @param employee*/@AutoFill(value = OperationType.UPDATE)void update(Employee employee);
自定义切面 AutoFillAspect
切入点
@Pointcut("execution(* com.sky.mapper.*.*(..)) &&@annotation(com.sky.annotation.AutoFill)")
拦截
com.sky.mapper包下的所有对象的所有方法,
用&& 追加:加了AutoFill 注解的方法
也就是:com.sky.mapper包下的所有对象的加了AutoFill 注解的方法
/*** Description 自定义切面,实现公共字段自动填充处理逻辑* Create by salvatore* Date 2023/11/1 14:36*/@Aspect
@Component
@Slf4j
public class AutoFillAspect {/*** 切入点*/@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")public void autoFillPointCut(){}/*** 前置通知,在通知中进行公共字段的赋值*/@Before("autoFillPointCut()")public void autoFill(JoinPoint joinPoint){/重要//可先进行调试,是否能进入该方法 提前在mapper方法添加AutoFill注解log.info("开始进行公共字段自动填充...");
// 如何通过aop 和 反射 来填充// 1. 获取到当前被拦截的方法上的数据库操作类型MethodSignature signature = (MethodSignature) joinPoint.getSignature();//切入点方法签名的对象AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);//获得方法上的注解对象OperationType operationType = autoFill.value();//获得数据库操作类型// 2. 获取到当前被拦截的方法的参数--实体对象Object[] args = joinPoint.getArgs();if(args == null || args.length == 0){return;}Object entity = args[0]; // 取第一个参数;// 3. 准备赋值的数据LocalDateTime now = LocalDateTime.now();Long currentId = BaseContext.getCurrentId();// 4. 根据当前不同的操作类型,为对应的属性通过反射来赋值if(operationType == OperationType.INSERT){//为4个公共字段赋值try {Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);//通过反射为对象属性赋值setCreateTime.invoke(entity,now);setCreateUser.invoke(entity,currentId);setUpdateTime.invoke(entity,now);setUpdateUser.invoke(entity,currentId);} catch (Exception e) {e.printStackTrace();}}else if(operationType == OperationType.UPDATE){//为2个公共字段赋值try {Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);//通过反射为对象属性赋值setUpdateTime.invoke(entity,now);setUpdateUser.invoke(entity,currentId);} catch (Exception e) {e.printStackTrace();}}}
}
更多推荐
SpringAOP 对公共字段自动填充处理
发布评论