首页

关于spring-beans包中AnnotationBeanUtils注解工具类进行实例对象和注释间相互转换代码示例

标签:AnnotationBeanUtils,注解工具类,spring-beans,注解用法,aop,springframework     发布时间:2018-09-04   

一、前言

关于spring-beans包中org.springframework.beans.annotation.AnnotationBeanUtils注解工具类,实现类对象或bean实例和注解实例间相互转换,从而加深对注解在代码解耦等场景用法示例。

二、源码说明

package org.springframework.beans.annotation;@b@@b@import java.lang.annotation.Annotation;@b@import java.lang.reflect.Method;@b@import java.util.Arrays;@b@import java.util.HashSet;@b@import java.util.Set;@b@import org.springframework.beans.BeanWrapper;@b@import org.springframework.beans.PropertyAccessorFactory;@b@import org.springframework.util.ReflectionUtils;@b@import org.springframework.util.StringValueResolver;@b@@b@public abstract class AnnotationBeanUtils@b@{@b@  public static void copyPropertiesToBean(Annotation ann, Object bean, String[] excludedProperties)@b@  {@b@    copyPropertiesToBean(ann, bean, null, excludedProperties);@b@  }@b@@b@  public static void copyPropertiesToBean(Annotation ann, Object bean, StringValueResolver valueResolver, String[] excludedProperties)@b@  {@b@    Set excluded = new HashSet(Arrays.asList(excludedProperties));@b@    Method[] annotationProperties = ann.annotationType().getDeclaredMethods();@b@    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean);@b@    Method[] arrayOfMethod1 = annotationProperties; int i = arrayOfMethod1.length; for (int j = 0; j < i; ++j) { Method annotationProperty = arrayOfMethod1[j];@b@      String propertyName = annotationProperty.getName();@b@      if ((!(excluded.contains(propertyName))) && (bw.isWritableProperty(propertyName))) {@b@        Object value = ReflectionUtils.invokeMethod(annotationProperty, ann);@b@        if ((valueResolver != null) && (value instanceof String))@b@          value = valueResolver.resolveStringValue((String)value);@b@@b@        bw.setPropertyValue(propertyName, value);@b@      }@b@    }@b@  }@b@}

三、用法示例

1. 定义两个测试注解annotation - @Class类作用域、@GoodBoy属性作用域

import java.lang.annotation.Documented;@b@import java.lang.annotation.Retention;@b@import java.lang.annotation.RetentionPolicy;@b@@b@@Retention(RetentionPolicy.RUNTIME)@b@@Documented@b@public @interface Class {@b@@b@	String className();@b@	@b@	int classId();@b@}
import java.lang.annotation.Documented;@b@import java.lang.annotation.Retention;@b@import java.lang.annotation.RetentionPolicy;@b@@b@@Retention(RetentionPolicy.RUNTIME)@b@@Documented@b@public @interface GoodBoy {@b@	@b@	int  score();@b@}

2. Student注入注解类

@Class(className="高三一班",classId=301)@b@public class Student {@b@	@b@	private  String className;@b@	@b@	private  int classId;@b@	@b@	@GoodBoy(score=100)@b@	private  int score;@b@@b@	public String getClassName() {@b@		return className;@b@	}@b@@b@	public void setClassName(String className) {@b@		this.className = className;@b@	}@b@@b@	public int getClassId() {@b@		return classId;@b@	}@b@@b@	public void setClassId(int classId) {@b@		this.classId = classId;@b@	}@b@@b@	public int getScore() {@b@		return score;@b@	}@b@@b@	public void setScore(int score) {@b@		this.score = score;@b@	}@b@@b@}

3. 测试入口类SpringAnnotationBeanUtilsTest

import org.springframework.beans.annotation.AnnotationBeanUtils;@b@@b@public class SpringAnnotationBeanUtilsTest {@b@	@b@	//测试方法注解@b@	public  static  void  testScore() throws  Exception{@b@		GoodBoy  goodboy=Student.class.getDeclaredField("score").getAnnotation(GoodBoy.class);@b@		Student  stuA=new Student();@b@		AnnotationBeanUtils.copyPropertiesToBean(goodboy, stuA);@b@		System.out.println("stuA同学成绩:"+stuA.getScore());@b@	}@b@	@b@	//测试类注解@b@	public  static  void  testClass() throws  Exception{@b@		Class  cla=Student.class.getAnnotation(Class.class);@b@		Student  stuB=new Student();@b@		AnnotationBeanUtils.copyPropertiesToBean(cla,stuB);@b@		System.out.println("stuB同学原班级编号:"+stuB.getClassId()+";所在班级为:"+stuB.getClassName());@b@		@b@		stuB.setClassName("高三二班");@b@		stuB.setClassId(302);@b@		@b@		System.out.println("stuB同学更换班级编号:"+stuB.getClassId()+";所在班级为:"+stuB.getClassName());@b@	}@b@	@b@	@b@	public static void main(String[] args)  throws  Exception{@b@		@b@		//测试方法注解@b@		testScore();@b@		@b@		//测试类注解@b@		testClass();@b@	}@b@	@b@}

控制台打印结果

stuA同学成绩:100@b@stuB同学原班级编号:301;所在班级为:高三一班@b@stuB同学更换班级编号:302;所在班级为:高三二班