java注解实现字典项属性赋值

1.编写字段属性的自定义注解

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DictParam {
    /**
     * 映射目标属性,为空默认直接映射到field,替换掉原来的field
     *
     * @return
     */
    String targetField() default "";

    /**
     * 映射来源属性
     *
     * @return
     */
    String field();

    /**
     * 数据字典类型
     *
     * @return
     */
    String dictType();
}

2.具体AOP实现

@Slf4j
@Aspect
@Component
public class DictAspect {

    public static final String GET = "get";

    public static final String SET = "set";

    @Pointcut("@annotation(com.citycloud.bigdata.biz.clearance.common.dict.DictParam)")
    private void dictHelper() {
    }

    @Around("dictHelper()")
    public Object doAround(ProceedingJoinPoint joinPoint) {
        try {
            // 执行方法得到结果
            Object result = joinPoint.proceed();
            //检查允许的字典项
            Signature signature = joinPoint.getSignature();
            MethodSignature methodSignature = (MethodSignature) signature;
            Method method = methodSignature.getMethod();
            if (method.isAnnotationPresent(DictParam.class)) {
                DictParam dictParam = method.getAnnotation(DictParam.class);
                String oldDictType = dictParam.dictType();
                String oldField = dictParam.field();
                String oldTargetField = dictParam.targetField();
                // 字典转换开始(使用反射)
                Class<?> clazz = result.getClass();
                // 反射调用get方法获取字段值
                Method sourceMethod = clazz.getMethod(GET + firstToUppercase(oldField));
                Object fieldValue = sourceMethod.invoke(result);
                if(Objects.isNull(fieldValue)){
                    return result;
                }
                // 获取字典值
                Map<String, DictVo> dictVoMap = DictConfig.DICT_MAPPER.get(oldDictType);
                if(dictVoMap.isEmpty()){
                    return result;
                }
                DictVo dictDo = dictVoMap.get(fieldValue.toString());
                // 获取目标方法进行设值
                String targetField = StringUtils.isBlank(oldTargetField) ? oldField : oldTargetField;
                Method targetMethod = clazz.getMethod(SET + firstToUppercase(targetField), dictDo.getClass());
                targetMethod.invoke(result, dictDo);
            }
            return result;
        } catch (Throwable throwable) {
            log.error("error:", throwable);
            return null;
        }
    }

    private String firstToUppercase(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
}

3.数据字典初始化

@Component
public class DictConfig {

    public static Map<String, Map<String, DictVo>> DICT_MAPPER = new HashMap<>();

    @Autowired
    private DictDao dictDao;


    @PostConstruct
    public void init() {
        List<DictDo> dictDoList = dictDao.selectList(new LambdaQueryWrapper<>(DictDo.class));
        List<DictVo> dictVoList = BeanCopyUtils.cloneFromList(dictDoList, DictVo.class);
        Map<String, Map<String, DictVo>> resultMap = dictVoList.stream().collect(Collectors.groupingBy(
                DictVo::getDictType, Collectors.toMap(DictVo::getDictValue, Function.identity(), (v1, v2) -> v2)));
        DICT_MAPPER.putAll(resultMap);
    }
}

4.字段上具体添加的注解

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值