当前位置:   article > 正文

自定义注解对bean中属性的注入和ConvertUtils动态转化String_javabean传输redis字段转string类型的注解

javabean传输redis字段转string类型的注解

借助于postProcessBeforeInitialization实现自定义注解对bean中属性的注入

postProcessAfterInitialization也可以,使用AopUtils判断代理类型并取得对应的代理对象,按需使用
使用ConvertUtils动态转化String为其他类型(支持列表查一下此工具类)
/**
 * @Description: 使用@Dict注解对Bean中String、List<String>和List<DictModel>类型对字段进行属性注入
 * 使用举例三种如下:
 * <span>
 * @Dict(dicCode = "oa_duty_status", dicText = "发布")
 * String dictValue / ? value; // value: "1"
 * </span>
 * <span>
 * @Dict(dicCode = "oa_duty_status")
 * List<String> / List<?> dictValueList; // value: ["0", "1"]
 * </span>
 * <span>
 * @Dict(dicCode = "oa_duty_status")
 * List<DictModel> dictModelList; value : [{value: "1", text: "发布"}, {value: "0", text: "暂存"}]
 * </span>
 * @author: xufeixiang
 * @date: 2022年01月10日 3:31 PM
 */
@Slf4j
@Component
public class DictItemsInjectProcessor extends InstantiationAwareBeanPostProcessorAdapter {

    @Autowired
    private CommonAPI commonAPI;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        injectDictItems(bean);
        return super.postProcessBeforeInitialization(bean, beanName);
    }

    private void injectDictItems(Object bean) {
        Class<?> beanClass = bean.getClass();
        List<Field> injectFieldList = FieldUtils.getFieldsListWithAnnotation(beanClass, Dict.class);
        for (Field field : injectFieldList) {
            Dict dictAnnotation = field.getAnnotation(Dict.class);
            String dicCode = dictAnnotation.dicCode();
            String dicText = dictAnnotation.dicText();
            List<DictModel> dictModelList = commonAPI.queryDictItemsByCode(dicCode);
            if (dictModelList.size() == 0) {
                continue;
            }

            if (StringUtils.isNotEmpty(dicText)) {
                List<String> dicTextList = Arrays.asList(StringUtils.split(dicText, StringPool.COMMA));
                dictModelList = dictModelList.stream()
                        .filter(x -> dicTextList.contains(x.getText()))
                        .collect(Collectors.toList());
            }

            boolean accessible = field.isAccessible();
            String targetTypeName = field.getType().getSimpleName(), targetRealType = targetTypeName;
            Object fieldValue;
            try {
                switch (targetTypeName) {
                    case "List":
                        Class<?> subTypeOfList = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
                        boolean isDictModelClass = StringUtils.equals(subTypeOfList.getName(), DictModel.class.getName());
                        fieldValue = dictModelList.stream()
                                .map(x -> isDictModelClass ? x : ConvertUtils.convert(x.getValue(), subTypeOfList))
                                .collect(Collectors.toList());
                        targetRealType += '<' + subTypeOfList.getSimpleName() + '>';
                        break;
                    case "Map":
                        Type[] typeArguments = ((ParameterizedType) field.getGenericType()).getActualTypeArguments();
                        Class<?> keyTypeOfMap = (Class<?>) typeArguments[0];
                        Class<?> valTypeOfMap = (Class<?>) typeArguments[1];
                        fieldValue = dictModelList.stream()
                                .collect(Collectors.toMap(x -> ConvertUtils.convert(x.getValue(), keyTypeOfMap),
                                        x -> ConvertUtils.convert(x.getText(), valTypeOfMap)));
                        targetRealType += '<' + keyTypeOfMap.getSimpleName() + ',' + valTypeOfMap.getSimpleName() + '>';
                        break;
                    case "String":
                        fieldValue = dictModelList.stream().map(DictModel::getValue).collect(Collectors.joining(StringPool.COMMA));
                        break;
                    default:
                        fieldValue = dictModelList.stream().map(DictModel::getValue).findAny().orElse(null);
                        fieldValue = ConvertUtils.convert(fieldValue, field.getType());
                        break;
                }

                if (!accessible) {
                    field.setAccessible(true);
                }

                field.set(bean, fieldValue);
                log.info("dictItems inject {}({}) for \"{}\" in \"{}\"", fieldValue, targetRealType, field.getName(), beanClass.getName());
            } catch (Exception e) {
                log.error("injectDictItems Exception", e);
            } finally {
                field.setAccessible(accessible);
            }
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/138572
推荐阅读
相关标签
  

闽ICP备14008679号