美文网首页
注解@annotation

注解@annotation

作者: getthrough | 来源:发表于2017-11-02 00:46 被阅读0次

注解如何生效

  1. 传入字节码对象,通过反射获取类, 字段, 方法上的注解对象
  2. 通过注解对象获取注解上的值
Car Info 注解
@Retention(RetentionPolicy.RUNTIME) //  在运行时仍然保留
@Target(ElementType.FIELD) // 在字段上使用注解
public @interface CarBrand {  // 汽车品牌------注解①
    String brand() default "";
}
--------------------------------------------------------------
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CarPrice {  // 汽车价格------注解②
    double price() default 0;
}
--------------------------------------------------------------
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CarColor {  // 汽车颜色------注解③
    enum color{BLACK, WHITE, RED, GREY};
    color carColor() default color.BLACK;
}
Car 对象上使用注解
public class Car {
    @CarBrand(brand = "BMW")
    private String brand;
    @CarPrice(price = 23333)
    private double price;
    @CarColor(carColor = CarColor.color.WHITE)
    private String color;

    public Car() {
    }

    public void run() {
        System.out.println("a "+color+" "+brand+ " car is driving");
    }
}

仅使用注解是无效的, 重要的是获取注解上的信息.

获取注解信息
public class CarInfoUtil {

    public static Map getCarInfo(Class<?> clazz) {
        String brand = "";  // 仅作声明用
        double price = 0;
        String color = "";

        Field[] declaredFields = clazz.getDeclaredFields();  // 获得所有字段
        for (Field f : declaredFields) {
            if (f.isAnnotationPresent(CarBrand.class)) {  // 如果当前的注解是参数中的类型
                CarBrand carBrand = f.getAnnotation(CarBrand.class);  // 获得注解
                brand = carBrand.brand();  // 取值
            }
            if (f.isAnnotationPresent(CarPrice.class)) {  // 同上
                CarPrice carPrice = f.getAnnotation(CarPrice.class);
                price = carPrice.price();
            }
            if (f.isAnnotationPresent(CarColor.class)) {
                CarColor carColor = f.getAnnotation(CarColor.class);
                color = carColor.carColor().toString();
            }
        }
        Map carInfo = new HashMap();
        carInfo.put("carBrand", brand);
        carInfo.put("carPrice", price);
        carInfo.put("carColor", color);
        return carInfo;
    }
}
使用注解信息
public class ResultTest {

    private Car car;

    @Before  // 该注解表示在测试方法之前执行,可用于初始化spring容器
    public void init() {
        car = new Car();
        Map carInfo = CarInfoUtil.getCarInfo(Car.class);
        car.setBrand((String) carInfo.get("carBrand"));
        car.setPrice((Double) carInfo.get("carPrice"));
        car.setColor((String) carInfo.get("carColor"));
    }

    @Test
    public void test1(){
        car.run();
    }
}

控制台输出:

a WHITE BMW car is driving

...

Tip:
使用注解时若只填写value()方法的值时, 括号中可省略value =, 直接写值; 除了value()方法还有其他方法需要设置值,那么value =不可省略,如:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CarBrand {
    String value() default "";
    String name() default "a";
}

那么可以这样写:

@CarBrand("BMW")
private String brand;

下面只能那样写:

@CarBrand(value = "BMW",name = "540i")
private String brand;

当然,首先得在注解中定义value()方法.

相关文章

网友评论

      本文标题:注解@annotation

      本文链接:https://www.haomeiwen.com/subject/rminpxtx.html