三步教你反射+注解自动装配对象

266 阅读1分钟
  1. 创建用来标注的注解类
import java.lang.annotation.*;

/**
 * 自定义一个注解
 */
/**
 * @Target:用于描述注解的使用范围
 * 类或接口:ElementType.TYPE;
 * 字段:ElementType.FIELD;
 * 方法:ElementType.METHOD;
 * 构造方法:ElementType.CONSTRUCTOR;
 * 方法参数:ElementType.PARAMETER。
 */
@Target(value={ElementType.FIELD,ElementType.METHOD})
/**
 * @Retention:表示需要在什么级别保存该注释信息,用于描述注解的生命周期
 * 仅编译期:RetentionPolicy.SOURCE;
 * 仅class文件:RetentionPolicy.CLASS;
 * 运行期:RetentionPolicy.RUNTIME。
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface DlyAnnotation {
    String value() default "default";
}
  1. 用来自动注入的bean
/**
 * 用来测试的bean
 */
public class DlyBean {
    @DlyAnnotation("Field")
    String name;

    @DlyAnnotation("Method")
    public void setName(String name){
        this.name=name;
    }

    @Override
    public String toString() {
        return "DlyBean{" +
                "name='" + name + ''' +
                '}';
    }
}
  1. 进行测试
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Text {
    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {
        /**
         * 创建一测试bean用来赋值
         */
        DlyBean text = new DlyBean();
        System.out.println(text);
        //1. 通过new对象实现反射机制( 对象.getClass() )
        //2. 通过路径实现反射机制( Class.forName("包名.类名") )
        //3. 通过类名实现反射机制 ( 类名.Class )
        //4. 通过类加载器xxxClassLoader.loadClass()传入类路径获取:通过类加载器获取 Class 对象不会进行初始化,意味着不进行包括初始化等一系列步骤,静态代码块和静态对象不会得到执行
        Class<DlyBean> dlyBeanClass = DlyBean.class;
        Method setName = dlyBeanClass.getMethod("setName",String.class);
        /**
         * 获取私有属性    getDeclaredField
         * 获取public属性 getField
         */
        Field name = dlyBeanClass.getDeclaredField("name");
        /**
         * 获取注解
         */
        DlyAnnotation annotationMethod = setName.getAnnotation(DlyAnnotation.class);
        DlyAnnotation annotationField = name.getAnnotation(DlyAnnotation.class);

//        System.out.println(annotationField.value());
//        System.out.println(annotationMethod.value());

//        name.setAccessible(true);  //对私有属性进行爆破
//        name.set(text,annotationField.value());
        setName.invoke(text,annotationMethod.value());

        System.out.println(text);
    }
}