反射和注解

本文详细介绍了Java中的反射机制,包括从配置文件读取类信息,动态创建实例和调用构造器。同时,文章讲解了自定义注解的创建、使用,以及如何结合反射在运行时检查和应用注解,实现自动化实例创建和属性赋值。示例代码展示了如何通过注解完成对象的构造和属性设置。
摘要由CSDN通过智能技术生成

反射

反射即 从配置文件中读取类的信息,java代码运行时才知道实际的类

package ref;

import java.io.IOException;
import java.util.Properties;

public class Test2 {
    private static Properties properties;

    static {
        properties = new Properties();
        try {
            properties.load(Test2.class.getResourceAsStream("../bean.properties"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws ClassNotFoundException {
        String bean = properties.getProperty("bean");
        Class clazz = Class.forName(bean);
        Constructor constructor = clazz.getConstructor();
        Object object = constructor.newInstance();
    }
}

步骤都是1.得到类对象,2.获得构造器,3.构造实例。

注解

作为标识符,通过反射实现功能。例如自动创建实例并赋值。

1.自定义注解

package ref;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyValue {
    String value();
}

package ref;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyComponent {
}

自定义注解需要添加原注解:

@Retention:注解生效时期

@Target:注解生效范围(给类/属性加)

2.给类加上自定义注解

package ref;

@MyComponent
public class Dog {
    @MyValue("大黄")
    private String name;
    @MyValue("2")
    private int age;
}
    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

3.通过注解完成对应的功能

package ref;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

public class Test {
    public static void main(String[] args) throws Exception {
        Class<Dog> dogClass = Dog.class;
        MyComponent annotation = dogClass.getAnnotation(MyComponent.class);
        if(annotation!=null){
            Constructor<Dog> constructor = dogClass.getConstructor();
            Dog dog = constructor.newInstance();
            Field[] declaredFields = dogClass.getDeclaredFields();
            for (Field declar:declaredFields){
                MyValue valueAnnotation = declar.getAnnotation(MyValue.class);
                if(valueAnnotation!=null){
                    declar.setAccessible(true);
                    if(declar.getType().getName().equals("int")){
                        Integer val = Integer.valueOf(valueAnnotation.value());
                        declar.set(dog,val);
                    }else{
                        declar.set(dog,valueAnnotation.value());
                    }
                }
            }
            System.out.println(dog);
        }

    }
}

第一步仍是获取类对象。
通过调用getAnnotation方法判断是否有相对应的注解。
仍旧是通过获取构造器去生成对象实例。
这里需要注意的是,给属性赋值时,由于时private修饰,故需要做暴力反射:
declar.setAccessible(true);

4.通过有参构造器构造实例

对象代码如下:

package ref;

@MyComponent
public class Dog {

    public Dog(String name) {
        this.name = name;
    }

    @MyValue("大黄")
    private String name;
    @MyValue("2")
    private int age;

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

那么在获取构造器时,需要指明是哪一个参数类型的构造器,并且在实例化的时候,也需要传入相对应的参数
如:

Class<Dog> dogClass = Dog.class;
        MyComponent annotation = dogClass.getAnnotation(MyComponent.class);
        if(annotation!=null){
            Constructor<Dog> constructor = dogClass.getConstructor(String.class);
            System.out.println("123");
            Dog aa = constructor.newInstance("aa");
            System.out.println(aa);
        }

参考视频:
反射

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值