1、注解
Java 注解用于为 Java 代码提供元数据(元数据是关于数据的数据。在编程语言上下文中,元数据是添加到程序元素如方法,字段,类和包上的额外信息。)
Annotation不直接影响程序的语义。开发和部署工具可以读取这些注释,并以某种形式处理这些注释,
可能生成其他的Java源程序、XML配置文件或者要与包含注释的程序一起使用的其他组件,从而影响运行状态的程序的语义。与javadoc不同,java的注解在编译时候可以嵌入到字节码文件中,注解可以被JVM保存和读取。
java共有十个内置注解3个存在于java.lang包中的常用注解、四个存在于java.lang.annotation包中的元注解以及后来新增的三个@SafeVarargs、@FunctionalInterface和@Repeatable
package java.lang.annotation;
public interface Annotation {
boolean equals(Object obj);
int hashCode();
String toString();
Class<? extends Annotation> annotationType();
}
//使用@interface定义注解时,便自动继承了该接口,
2、常见的内置注解
@Override
@Target(value=METHOD)
@Retention(value=SOURCE)
public @interface Override{}
表示方法声明旨在覆盖超类型中的方法声明。如果使用此注释类型注释方法,则除非至少满足以下条件之一,否则需要编译器生成错误消息:
- 该方法将覆盖或实现在超类型中声明的方法。
- 该方法具有与Object中声明的任何公共方法的覆盖相同的签名 。
@Deprecated
@Documented
@Retention(value=RUNTIME)
@Target(value={CONSTRUCTOR,字段,LOCAL_VARIABLE,METHOD,PACKAGE,PARAMETER,TYPE})
public @interface Deprecated{}
注释@Deprecated的程序元素是程序员不鼓励使用的程序元素,用于标明被修饰的类或类成员、类方法已经废弃、过时,不建议使用。
@SuppressWarnings
@Target(value={TYPE,字段,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
@Retention(value=SOURCE)
public @interface SuppressWarnings{
String[] value();
}
表示在注释元素(以及注释元素中包含的所有程序元素)中应该抑制命名的编译器警告。用于关闭对类、方法、成员编译时产生的特定警告。
@FunctionalInterface
从java8开始支持,标识一个匿名函数或函数式接口
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {}
3、meta-annotation元注解
-
@Target
标记注解的作用域,是哪种 Java 成员使用。
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); } //参数为ElementType枚举类型 public enum ElementType { TYPE, //类、接口(包括注释类型)或枚举声明 METHOD,//在方法上声明注解 FIELD,//在字段上声明注解(包括枚举常量) PARAMETER,//在参数上声明注解 LOCAL_VARIABLE,//在局部变量上声明注解 PACKET,//在包上声明注解 CONSTRUCTOR,//在构造方法上声明注解 ANNOTATIONTYPE,//在注解类型上声明注解 }
-
@Retention
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { RetentionPolicy value(); }
指示要保留接口的注释持续时间。
可选变量为RetentionPolicy.SOURCE),注解仅存在于源码中,在class字节码文件中不包含
RetentionPolicy.CLASS)注解会在class字节码文件中存在,但运行时无法获得
RetentionPolicy.RUNTIME:注解会在class字节码文件中存在,在运行时可以通过反射获取到
保留策略默认为RetentionPolicy.CLASS
-
@Documented
将注解中的元素包含到 Javadoc 中去。
-
@Inherited
一个被@Inherited注解了的注解的类,如果他的子类没有被其他注解修饰,则它的子类也继承了父类的注解。
-
@Repeatable
可以同时作用一个对象多次,但是每次作用注解又可以代表不同的含义。
4、自定义注解
使用@interface自定义注解,便自动继承了java.lang.annotation.Annotation接口,即该注解就是一个Annotation
注解本身就是Annotation接口的子接口,也就是说注解中其实是可以有属性和方法,但是接口中的属性默认都是static final的,而我们定义接口的方法就相当于自定义注解含有的属性。
@TableUser("db_user")//参数只有一个可以省略参数名=
public class User {
@FiledUser(columnName = "id",type ="int",length=10)
private int id;
@FiledUser(columnName = "name",type ="varchar",length=3)
private String name;
@FiledUser(columnName = "gender",type ="boolean",length=10)
public boolean gender;
public User(int id, String name, boolean gender) {
this.id = id;
this.name = name;
this.gender = gender;
}
}
}
//类注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableUser{
String value();
}
//属性注解
@Target(value = {ElementType.FIELD})//数组参数用{}
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface FiledUser{
String columnName();
String type() default "varchar";//设置默认参数
int length();
}