详述Java元注解

Target

@Target:用于指定被修饰的自定义注解只能用于修饰程序中哪些元素,该元注解有如下属性值:
ElementType.ANNOTATION_TYPE:应用于其他注解的元注解
ElementType.CONSTRUCTOR:应用于构造函数
ElementType.FIELD:应用于全局属性
ElementType.LOCAL_VARIABLE:应用于方法中的本地变量
ElementType.METHOD:应用于方法
ElementType.PACKAGE:应用于包
ElementType.PARAMETER:应用于方法的参数
ElementType.TYPE:应用于类、接口或者枚举声明

代码示例:

package venus;

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

@Target({ElementType.FIELD,ElementType.METHOD})
public @interface Autowired {

}

则Autowired注解可使用在方法和全局属性上。

package venus;

public class Test {
    @Autowired
    private String name;

    @Autowired
    public static void main(String[] args) {
        
    }
}

Retention

@Retention:用于指定被修饰的自定义注解可以保留多久,该元注解有如下属性值:
RetentionPolicy.SOURCE:编译器将直接丢弃被修饰的注解
RetentionPolicy.CLASS:默认值,编译器将把注解记录在class文件中,当运行Java程序时,虚拟机不再保留注解
RetentionPolicy.RUNTIME:编译器将把注解记录在class文件中,当运行java程序时,虚拟机保留注解,程序可以通过反射获取该注解
代码示例:

package venus;

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

@Target({ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.CLASS)
public @interface Autowired {


}

则注解记录仍然保留在class文件中,但当运行程序时该注解被删除。

package venus;


import java.lang.reflect.Field;

public class Test {
    @Autowired
    private String name;

    @Autowired
    public static void main(String[] args) {
        Class clazz = Test.class;
        try{
            Field field = clazz.getDeclaredField("name");
            Autowired autowired = field.getAnnotation(Autowired.class);
            System.out.println(autowired);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

由于运行时注解被删除,故输出null;
但若修改Retention注解属性:

package venus;

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

@Target({ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired {


}

则输出@venus.Autowired()。

Documented

@Documented:执行javadoc命令时,被该元注解修饰的自定义注解也会生成在文档中
代码示例:

package venus;

import java.lang.annotation.Documented;

@Documented
public @interface Autowired {

    String dis();
}

package venus;

@Autowired(dis = "这是描述信息")
public class Test {

}

若没有Documented注解,则生成文档中没有@Autowired注解。

Inherited

@Inherited:如果父类所使用的注解有@Inherited修饰,则子类可以继承该注解,否则不能继承。
创建两个类

package venus;

@Autowired
public class Father {
}

package venus;

public class Son extends Father{
}

Son类继承Father类。

package venus;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Autowired {

}

package venus;

import java.lang.annotation.Annotation;

@Autowired
public class Test {
    public static void main(String[] args) {
        Class clazz = Son.class;
        Annotation annotation = clazz.getAnnotation(Autowired.class);
        System.out.println(annotation);
    }

}

由于Retention默认值为CLASS,故需要设置属性值。因此输出为@venus.Autowired(),表面Son类继承了该注解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值