Sping Boot 自定义注解使用示例

自定义类注解

示例为自定义类注解使用 type 值获取对应类下的方法执行后的结果返回。

@Inherited
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationClassAnnotation {
    UserOperationType operationType() default UserOperationType.ONE_CODE;
}

使用枚举类

@Getter
@AllArgsConstructor
public enum UserOperationType {

    ONE_CODE("1", "1"),
    TWO_CODE("2", "2"),
    THREE_CODE("3", "3"),
    FOUR_CODE("4", "4"),
    ;

    private String key;
    private String value;

}

使用策略

    @Autowired
    private ApplicationContext applicationContext;
    private Map userOperationProcessorMap = null;

    private void initProcessor() {
        //获取到使用了OperationClassAnnotation注解的类
        Map controllers = applicationContext.getBeansWithAnnotation(OperationClassAnnotation.class);

        for (Map.Entry entry : controllers.entrySet()) {
            Object value = entry.getValue();

            Class aClass = value.getClass();
            if (UserOperationProcessor.class.isAssignableFrom(aClass)) {
                // 获取当前对象 声明的注解 获取到注解后 还可以获取注解中的属性
                OperationClassAnnotation operationClassAnnotation = aClass.getAnnotation(OperationClassAnnotation.class);
                userOperationProcessorMap.put(operationClassAnnotation.operationType().getKey(), (UserOperationProcessor) value);
            }
        }
    }

    /**
     * 根据key值获取处理类
     *
     * @param type
     * @return
     */
    private UserOperationProcessor getProcessorByType(String type) {

        //初始化
        if (CollectionUtils.isEmpty(userOperationProcessorMap)) {
            userOperationProcessorMap = new HashMap<>();
            initProcessor();
        }

        if (userOperationProcessorMap.containsKey(type)) {
            return userOperationProcessorMap.get(type);
        }
        return null;
    }

    public Page findUserStatistics(ManPowerCommonQuery param) {

        UserOperationProcessor processorByType = getProcessorByType(param.getQueryType());
        if (Objects.isNull(processorByType)) {
            return new Page(param.getCurrentPage(), param.getPageSize(), 0L);
        }
        return processorByType.getUserStatistics(param);
    }

后续类实现 processorByType 接口下的 getUserStatistics 方法即可解析到对应类型方法。

自定义方法注解

自定义方法适用于 aop 切面方法监听,不建议使用于解析方法或者反射方法执行。

@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RecordAnnotation {
    NursingRecordOperationType saveLogType() default NursingRecordOperationType.ADD;
}

使用 AOP 切面示例

@Aspect
@Component
@Slf4j
public class RecordAspect {

    private final NursingRecordLogService nursingRecordLogService;

    public RecordAspect(NursingRecordLogService nursingRecordLogService) {
        this.nursingRecordLogService = nursingRecordLogService;
    }

    @Pointcut("@annotation(com.depth.RecordAnnotation)")
    private void saveLog() {
    }

    @Around("saveLog() && @annotation(recordAnnotation)")
    public Object doSaveLog(ProceedingJoinPoint joinPoint, RecordAnnotation recordAnnotation) throws Throwable {

        // 获取方法返回值
        Object proceed = joinPoint.proceed();
        // 返回值验证
        if (Objects.isNull(proceed)) {
            return null;
        }
        try {
            // 保存类型枚举
            NursingRecordOperationType nursingRecordOperationType = recordAnnotation.saveLogType();
            // 构建日志信息保存
            NursingRecordVo nursingRecordVo = new NursingRecordVo();
            nursingRecordVo.setOperationType(nursingRecordOperationType);
          。。。。。。

        } catch (Exception e) {
            log.error("日志保存失败.2--type:" + recordAnnotation.saveLogType().getValue(), e);
        }

        return proceed;
    }

自定义字段注解

自定义字段注解用于 Vo 对象字典值赋值使用,可正向解析或逆向解析,字典值 -> 字典名称 或 字典名称 -> 字典值。

@Documented
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DictionaryField {

    DictionaryVariable dictionaryType() default DictionaryVariable.SYS_SEX;

}

使用字典枚举类

@Getter
@AllArgsConstructor
public enum DictionaryVariable {

    SYS_SEX("sys_sex", "性别(1:男2:女)"),
    SYS_EDUCATION_TYPE("sys_education_type", "学历")
    ;

    private String key;
    private String value;
}

使用方式

    /**
     * 性别
     */
    @DictionaryField(dictionaryType = DictionaryVariable.SYS_SEX)
    private String sexId;

渲染方法

    /**
     * 渲染字典属性值
     * (仅能渲染本级与父级)
     * @param params
     * @param resolving 当此值不为空则为反解析
     * @return void
     * @throws
     * @method initDictionaryField
     * @version 1.0
     */
    public  void initDictionaryField(List params, Object... resolving) {

        // 1. 获取注解属性中的字典组信息
        Class dictionaryVariableClass = DictionaryVariable.class;
        DictionaryVariable[] enumConstants = dictionaryVariableClass.getEnumConstants();
        List dictionaryArr = new ArrayList<>();
        for (int i = 0; i < enumConstants.length; i++) {
            DictionaryVariable enumConstant = enumConstants[i];
            dictionaryArr.add(enumConstant.getKey());
        }
        GostopResponseVo>> cacheList = common.findCacheList(dictionaryArr.stream().toArray(String[]::new));
        Map> dictDataMap = cacheList.getResult();
        if (Objects.isNull(dictDataMap)) {
            return;
        }

        // 2. 获取需要设置的字段信息
        params.stream().forEach(obj -> {
            Class clazz = obj.getClass();
            Field[] declaredFields = clazz.getDeclaredFields();
            // 2.1 设置当前类属性字段
            setFieldAttr(declaredFields, dictDataMap, obj, resolving);

            // 2.2 获取是否继承父级
            Class parent = clazz.getSuperclass();
            if(null == parent){
                return;
            }
            declaredFields = parent.getDeclaredFields();
            // 2.3 设置父级字段
            setFieldAttr(declaredFields, dictDataMap, obj, resolving);
        });
    }

    /**
     * 设置反射类字段值
     *
     * @param declaredFields
     * @param dictDataMap
     * @param obj
     * @param resolving
     * @return void
     * @exception
     * @method setFieldAttr
     * @version 1.0
     */
    private  void setFieldAttr(Field[] declaredFields, Map> dictDataMap, T obj, Object... resolving){
        // 1、参数验证
        if(CollectionUtils.isEmpty(declaredFields)){
            return;
        }

        // 2、字段赋值
        for (Field declaredField : declaredFields) {
            DictionaryField annotation = declaredField.getAnnotation(DictionaryField.class);
            if (null == annotation) {
                continue;
            }
            String key = annotation.dictionaryType().getKey();
            List dictDataVos = dictDataMap.get(key);
            if (CollectionUtils.isEmpty(dictDataVos)) {
                continue;
            }
            declaredField.setAccessible(true);
            try {
                Object valueObj = declaredField.get(obj);
                if (Objects.isNull(valueObj)) {
                    continue;
                }

                // 3. 赋值字典名称
                String value = BeanUtil.nullOrUndefinedToEmptyStr(valueObj);
                if (StringUtils.isEmpty(value)) {
                    continue;
                }

                // 反解析字典数据
                if (CollectionUtils.isNotEmpty(resolving)) {
                    DictDataVo dictDataVo = dictDataVos.stream().filter(e -> e.getDictionaryName().equals(value)).findFirst().orElse(null);
                    if (Objects.isNull(dictDataVo)) {
                        continue;
                    }
                    declaredField.set(obj, dictDataVo.getDictionaryValue());
                    continue;
                }

                DictDataVo dictDataVo = dictDataVos.stream().filter(e -> e.getDictionaryValue().equals(value)).findFirst().orElse(null);
                if (Objects.isNull(dictDataVo)) {
                    continue;
                }
                declaredField.set(obj, dictDataVo.getDictionaryName());
            } catch (IllegalAccessException e) {
                throw new BusinessException(CenterConstant.DICTIONARY_FIELD_ERROR);
            }
        }

    }

你可能感兴趣的:(自定义注解,java,spring,boot,开发语言,spring,cloud,spring)