公共字段自动填充

本文详细介绍了如何使用AOP注解和反射技术在Java中实现自定义注解AutoFill,解决公共字段的自动填充问题,包括自定义注解的创建、切面类的定义、切入点和通知的配置,以及在Mapper方法中添加注解的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、涉及技术点

        技术点:AOP 注解 反射 枚举

        自定义注解+AOP处理公共字段问题

二、实现步骤——自定义注解

        自定义注解AutoFill,标识需要进行公共字段自动填充的方法

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

三、实现步骤——自定义切面类

        自定义切面类AutoFillAspect,统一拦截加入了AutoFillAspect自定义注解的方法,并通过反射为公共字段赋值

        1.定义切入点和通知

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

    /**
     * 前置通知:为公共字段赋值
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("开始进行公共字段自动填充...");
}

        2.获取数据

//获取当前方法上拦截的数据库操作类型
        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 localDateTime = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();

        3.通过反射为公共字段赋值

  //根据不同的操作类型,通过反射为对应的属性赋值
        if (operationType == OperationType.INSERT) {
            //为四个公共字段赋值
            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, localDateTime);
                setCreateUser.invoke(entity, currentId);
                setUpdateTime.invoke(entity, localDateTime);
                setUpdateUser.invoke(entity, currentId);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(operationType == OperationType.UPDATE){
            //为两个公共字段赋值
            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,localDateTime);
                setUpdateUser.invoke(entity,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

四、实现步骤——在Mapper方法添加自定义注解

        在mapper的方法上加入自定义注解AutoFill

    @Insert
    @AutoFill(OperationType.INSERT)
    void insert(Employee employee);

在“苍穹外卖”系统中实现公共字段自动填充功能,主要依赖于 Spring AOP(面向切面编程)和自定义注解。该功能的核心思想是在执行数据库操作前,通过拦截带有特定注解的方法,使用反射机制为实体类中的公共字段自动赋值,避免在每个业务逻辑中重复编写相同的赋值代码。 ### 实现步骤如下: #### 1. 定义自定义注解 `@AutoFill` 首先创建一个自定义注解,用于标记需要进行公共字段自动填充的方法。 ```java @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AutoFill { } ``` #### 2. 创建切面类 `AutoFillAspect` 接下来编写一个切面类,使用 Spring AOP 拦截所有被 `@AutoFill` 注解标记的方法,并在方法执行前自动填充公共字段。 ```java @Aspect @Component @Slf4j public class AutoFillAspect { /** * 定义切入点:拦截所有 service 包下的方法,并且这些方法必须有 @AutoFill 注解 */ @Pointcut("execution(* com.sky.service.*.*(..)) && @annotation(com.sky.annotation.AutoFill)") public void autoFillPointcut() {} /** * 在目标方法执行前进行公共字段填充 */ @Before("autoFillPointcut()") public void autoFill(JoinPoint joinPoint) { log.info("开始公共字段自动填充"); // 获取方法参数中的实体对象 Object[] args = joinPoint.getArgs(); if (args == null || args.length == 0) { return; } // 假设第一个参数为实体对象 Object entity = args[0]; // 使用反射设置公共字段值 try { // 获取当前登录用户ID(示例) Long currentUserId = getCurrentUserId(); // 设置更新人字段 Method setUpdateUser = entity.getClass().getMethod("setUpdateUser", Long.class); setUpdateUser.invoke(entity, currentUserId); // 设置创建人字段(如果是新增操作) Method getCreateTime = entity.getClass().getMethod("getCreateTime"); if (getCreateTime.invoke(entity) == null) { Method setCreateUser = entity.getClass().getMethod("setCreateUser", Long.class); setCreateUser.invoke(entity, currentUserId); } } catch (Exception e) { log.error("公共字段填充失败", e); } } /** * 获取当前登录用户 ID(需结合项目实际获取方式) */ private Long getCurrentUserId() { // 示例返回固定值,实际应从 ThreadLocal 或 SecurityContext 中获取 return 1L; } } ``` #### 3. 在实体类中定义公共字段 确保实体类中包含常见的公共字段,如创建时间、创建人、更新时间、更新人等,并提供相应的 getter 和 setter 方法。 ```java public class Dish { private Long id; private String name; private LocalDateTime createTime; private LocalDateTime updateTime; private Long createUser; private Long updateUser; // Getter and Setter methods } ``` #### 4. 在 Service 层方法上添加 `@AutoFill` 注解 最后,在需要自动填充公共字段的业务方法上添加 `@AutoFill` 注解,Spring AOP 将会自动拦截并执行填充逻辑。 ```java @Service public class DishService { @AutoFill public void save(Dish dish) { // 执行保存逻辑 } @AutoFill public void update(Dish dish) { // 执行更新逻辑 } } ``` --- ### 注意事项 - **ThreadLocal 用户信息**:为了获取当前登录用户的信息,通常会使用 `ThreadLocal` 来存储用户上下文,确保线程安全[^3]。 - **异常处理**:在实际部署时,应完善日志记录和异常捕获机制,确保系统的健壮性。 - **性能优化**:如果数据量较大或并发较高,可以考虑对反射操作进行缓存,提升性能。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wei@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值