注解(Annotation)在JDK1.5之后增加的一个新特性, 使用注解可以极大地简化编码提高开发效率,目前许多流行的框架(如Spring、MyBatis等)都大量使用注解。
编写注解类:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Author {
String name();
String email();
}
编写Item类测试注解:
@Author(name = "finley", email = "finley@example.com")
public class Item {
public static void main(String[] args) {
if (Item.class.isAnnotationPresent(Author.class)) {
Author author = Item.class.getAnnotation(Author.class);
System.out.println("author: " + author.name() + ", email: " + author.email());
}
}
}
上述示例在运行中获得了Item类被@Author注解的信息即注解中的参数。
工程开发中可以使用注解提供元信息,如Spring框架中@Service、@Configuration注解标注类的角色,@Profile、@Import注解标注类的元信息。
注解只是标记了元素的元信息,并不会直接影响代码的执行过程。
Class对象还提供了getAnnotations()等方法,可以参考JavaDoc。
元注解是标记注解类型的注解,上文在定义注解时使用的@Target和@Retention即为元注解。
@Target标记注解元素类型, 由ElementType枚举定义:
TYPE: 修饰类(class)、接口(interface)和枚举(enum)FIELD: 修饰域和枚举中定义的枚举常量METHOD: 修饰方法PARAMETER: 修饰方法的参数,如SpringMVC的@PathVariable注解CONSTRUCTOR: 修饰构造器LOCAL_VARIABLE: 修饰局部变量ANNOTATION_TYPE: 修饰注解类,即元注解PACKAGE: 修饰包通常注解只能标记在声明处不能标记在使用处,如Method型注解只能标注在方法定义处不能标注在方法调用处。
在Java8之后Type注解可以标记在类型声明或使用处, Java8新增了两个ElementType:
TYPE_PARAMETER: 可以标记TYPE定义, 与1.8之前的TYPE相同TYPE_USE: 可以标记TYPE使用,包括: 初始化对象时(new),对象类型转换, implements/extends声明,throws声明可以使用@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})指定两种Target。
@Retention标记注解被保留的时长, 由RetentionPolicy枚举定义:
SOURCE: 仅保留在源文件中CLASS: 保留在源文件和编译生成的.class字节码文件中,但在运行时不再保留。 默认保留策略。RUNTIME: 保留在源文件和编译生成的.class字节码文件以及运行时VM中,可以使用反射机制在运行时获得注解信息@Inherited表示注解可以被子类继承, 首先在基类上标记 Inherited 的注解:
@Author(name = "finley", email = "finley@example.com")
public class Base {
}
Item类没有使用@Author但它继承了基类的标记,因此我们可以获得@Author注解信息
public class Item extends Base {
public static void main(String[] args) {
if (Item.class.isAnnotationPresent(Author.class)) {
Author author = Item.class.getAnnotation(Author.class);
System.out.println("author: " + author.name() + ", email: " + author.email());
}
}
}
@Documented表示被标记的类型可以被JavaDoc工具文档化。
在Java8之前同一个元素不能被相同的注解重复标记,@Repeatable允许一个注解重复标记同一个对象。
编写带有@Repeatable声明的注解:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(AuthorContainer.class)
public @interface Author {
String name();
String email();
}
编写Container注解:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthorContainer {
Author[] value();
}
AuthorContaier必须提供声明方法Author[] value();,它的保留时间要长于Author且Target类型必须是Author目标类型的子集。
重复使用@Author标记:
@Author(name = "finley", email = "finley@example.com")
@Author(name = "finley2", email = "finley2@example.com")
public class Item extends Base {
public static void main(String[] args) {
if (Item.class.isAnnotationPresent(AuthorContainer.class)) {
AuthorContainer authorContainer = Item.class.getAnnotation(AuthorContainer.class);
for (Author author : authorContainer.value()) {
System.out.println("author: " + author.name() + ", email: " + author.email());
}
}
}
}
两次@Author标记被解释为@AuthorContainer标记,可以通过authorContainer.value()来访问每个@Author标记的信息。
注意Item.class.isAnnotationPresent(Author.class)会被判定为false。
原文:https://www.cnblogs.com/Finley/p/9325763.html