以前没有接触过注解开发,最近在学习某站开发视频的时候碰到了,特此记录一下以期加深理解
由于所学不精,很多理解都浮在表面,可能对各位帮助性不大,这里还是以记录为主。
应用场景
在多个方法有公共操作时,可以进行抽取,通过注解的方式减少代码量
本文案例:在更新或加入菜品和员工信息时,需要同时对更新时间,更新人工号进行更新,创建时还额外需要添加创建人工号,创建时间。这些都属于重复操作,因此可以在进行提取。
注解应用地点:公共方法之前
应用形式:
@AutoFill(value = OperationType.INSERT)
void insert(Category category);
/**
* 根据id修改分类
* @param category
*/
@AutoFill(value = OperationType.UPDATE)
void update(Category category);
由于这里插入操作和更新操作有所不同,因此需要增加value = OperationType.UPDATE 来进行分辨以对应不同操作。
实现流程
步骤一
自定义注解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
//数据库操作类型:UPDATE INSERT
OperationType value();
}
@Target(ElementType.METHOD)用于确定注解应用范围,这里表示只能用于方法
@Retention(RetentionPolicy.RUNTIME)该注解用于表示这个注解方法可以在运行时使用。
@interface 用于声明这是一个注解
OperationType如下:
public enum OperationType {
/**
* 更新操作
*/
UPDATE,
/**
* 插入操作
*/
INSERT
}
自定义切面
通过切面编程的方式在切点处插入所需要的增强的方法,插入方法操作通过反射实现
定义解析均写于之后的代码上
/**
* 自定义切面,实现公共字段自动填充处理逻辑
*/
//用于表示这是一个切面
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
/**
* 切入点, 在这里相当于被切入的方法
* @Pointcut用于指定一个切入点,即被拦截的方法
* execution用于表示切入点涉及的范围,这里是在sql语句执行前进行处理,因此在mapper包下
* @annotation表示被切入的方法需要有AutoFill注解
*/
@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
public void autoFillPointCut(){}
/**
* 前置通知,在通知中进行公共字段的赋值
* @Before是AspectJ框架的注解,用于声明一个前置通知方法
* 这里表示@Before注解标记的方法将在autoFillPointCut()所匹配的方法之前执行
* JoinPoint代表当前被拦截的方法的参数信息,包括方法签名、参数、返回值等。
*/
@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();
}
}
}
}
步骤三
应用注解
@AutoFill(value = OperationType.INSERT)
void insert(Category category);
/**
* 根据id修改分类
* @param category
*/
@AutoFill(value = OperationType.UPDATE)
void update(Category category);

被折叠的 条评论
为什么被折叠?



