AnnotationBeanUtils

https://www.cnblogs.com/liferecord/p/7255375.html


    

public abstract class AnnotationBeanUtils {
    public AnnotationBeanUtils() {
    }

    public static void copyPropertiesToBean(Annotation ann, Object bean, String... excludedProperties) {
        copyPropertiesToBean(ann, bean, (StringValueResolver)null, excludedProperties);
    }


/**
     * Copy the properties of the supplied {@link Annotation} to the supplied target bean.
     * Any properties defined in {@code excludedProperties} will not be copied.
     * <p>A specified value resolver may resolve placeholders in property values, for example.
      将提供的{@link Annotation}的属性复制到提供的目标bean。
       {@code excludedProperties}中定义的任何属性都不会被复制。
      <p>例如,指定的值解析程序可以解析属性值中的占位符。
     * @param ann the annotation to copy from
     * @param bean the bean instance to copy to
     * @param valueResolver a resolve to post-process String property values (may be {@code null})
     * @param excludedProperties the names of excluded properties, if any
     * @see org.springframework.beans.BeanWrapper
     */
    public static void copyPropertiesToBean(Annotation ann, Object bean, StringValueResolver valueResolver, String... excludedProperties) {
        Set<String> excluded = new HashSet(Arrays.asList(excludedProperties));
        Method[] annotationProperties = ann.annotationType().getDeclaredMethods();//获取注解上的所有方法
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean);//通过bean的对象获取bean的定义beandefinition
        Method[] var7 = annotationProperties;
        int var8 = annotationProperties.length;

        for(int var9 = 0; var9 < var8; ++var9) {//遍历所有方法
            Method annotationProperty = var7[var9];
            String propertyName = annotationProperty.getName();//获取方法的名字
            if (!excluded.contains(propertyName) && bw.isWritableProperty(propertyName)) {
                Object value = ReflectionUtils.invokeMethod(annotationProperty, ann);//获取注解上属性的值
                if (valueResolver != null && value instanceof String) {
                    value = valueResolver.resolveStringValue((String)value);//对字符串进行处理
                }
                bw.setPropertyValue(propertyName, value);//把属性设置到对象上去
            }
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值