AOP切面自动填充公共字段

AOP切面自动填充公共字段

1. 定义枚举类

package com.sky.enumeration;

/**
* 数据库操作类型
*/
public enum OperationType {

/**
* 更新操作
*/
UPDATE,

/**
* 插入操作
*/
INSERT

}

2. 创建自定义注解类

package com.sky.annotation;

/**
* @author Dear_Monster
* @create 2025-10-24 22:25
*/

import com.sky.enumeration.OperationType;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 自定义注解,用于标识某个方法需要进行功能字段自动填充处理
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
//数据库操作类型:UPDATE INSERT
OperationType value();
}

3. 定义常量类

package com.sky.constant;

/**
* 公共字段自动填充相关常量
*/
public class AutoFillConstant {
/**
* 插入操作相关字段
*/
public static final String SET_CREATE_TIME = "setCreateTime";
public static final String SET_CREATE_USER = "setCreateUser";
/**
* 更新操作相关字段
*/
public static final String SET_UPDATE_TIME = "setUpdateTime";
public static final String SET_UPDATE_USER = "setUpdateUser";
}

4. 使用AOP技术实现公共代码的赋值

package com.sky.aspect;

/**
* @author Dear_Monster
* @create 2025-10-24 22:27
*/


import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
* 自定义切面实现公共字段自动填充处理逻辑
*/
@Aspect // 告诉Spring这是一个AOP切面类
@Component // 告诉Spring要创建并管理这个Bean
@Slf4j // Lombok自动提供log对象用于日志记录
public class AutoFillAspect {

/**
* 切入点
*/
@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
public void autoFillPointCut() {
}

/**
* 设置通知:在通知中进行公共字段的赋值
*/
@Before("autoFillPointCut()")
public void autoFill(JoinPoint joinpoint) {
log.info("开始进行公共字段自动填充...");

//获取到当前被拦截的方法上的数据库操作类型(获取方法上的注解的信息)
MethodSignature signature = (MethodSignature) joinpoint.getSignature();
AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
OperationType operationType = autoFill.value();//获得数据库操作类型

//获取到当前被拦截的方法的参数...实体对象
Object[] args = joinpoint.getArgs();//获取被拦截方法的所有参数
if (args == null || args.length == 0) {
return;//结束当前方法 不再执行后续代码
}

Object entity = args[0];

//准备赋值的数据
LocalDateTime now = LocalDateTime.now();
Long currentId = BaseContext.getCurrentId();

//根据当前不同的操作类型,为对应的属性通过反射来赋值
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();
}

}
}
}

5. 使用示例

在Mapper接口方法上添加自定义注解:

@AutoFill(OperationType.INSERT)
void insert(Category category);

@AutoFill(OperationType.UPDATE)
void update(Category category);

注意事项

  1. 此实现使用反射机制为对象字段赋值,需要实体类提供标准的setter方法
  2. 依赖BaseContext类获取当前登录用户ID,需要确保该类已正确实现
  3. 常量类AutoFillConstant定义了固定的方法名,实体类中必须使用这些标准命名
  4. 切入点表达式限定了拦截范围在mapper包下的方法