欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 会展 > 【SpringBoot】SpringAOP实现公共字段自动填充

【SpringBoot】SpringAOP实现公共字段自动填充

2024/10/23 15:33:37 来源:https://blog.csdn.net/qq_45722630/article/details/140576196  浏览:    关键词:【SpringBoot】SpringAOP实现公共字段自动填充

1. 自定义注解annotation

@Target 指定可以使用注解的类型
@Retention 指定合适使用
value指定注解值得类型

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 {// 操作类型OperationType value();
}

2. 切面aspect

@Pointcut(“execution()”) 定义切入点
@Before(“pointcut()”) 在切入点之前执行方法

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.entity.Employee;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
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.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.LocalTime;@Aspect
@Component
@Slf4j
public class AutoFillAspect {/*** 切入点*/@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")public void autoFillAspectPointcut(){}/*** 切入点之前执行*/@Before("autoFillAspectPointcut()")public void jointPoint(JoinPoint joinPoint) {// 获取注解里面的值MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();// 获取注解AutoFill annotation = methodSignature.getMethod().getAnnotation(AutoFill.class);OperationType operationType = annotation.value();// 获取参数Object[] args = joinPoint.getArgs();if (args==null || args.length==0) {return;}// 获取更新数据LocalDateTime time = LocalDateTime.now();Long userId = BaseContext.getCurrentId();Object entity = args[0];// 根据类型进行更新if (OperationType.INSERT.equals(operationType)) {// 反射进行更新try {Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);setCreateTime.invoke(entity, time);setCreateUser.invoke(entity, userId);} catch (NoSuchMethodException e) {throw new RuntimeException(e);} catch (InvocationTargetException e) {throw new RuntimeException(e);} catch (IllegalAccessException e) {throw new RuntimeException(e);}} else if (OperationType.UPDATE.equals(operationType)) {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, time);setUpdateUser.invoke(entity, userId);} catch (NoSuchMethodException e) {throw new RuntimeException(e);} catch (InvocationTargetException e) {throw new RuntimeException(e);} catch (IllegalAccessException e) {throw new RuntimeException(e);}}}
}

3. 使用自定义注解

    @AutoFill(OperationType.UPDATE)void update(Employee employee);

4. 枚举类型

/*** 数据库操作类型*/
public enum OperationType {/*** 更新操作*/UPDATE,/*** 插入操作*/INSERT}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com