注解(Annotation)
- Java 注解用于为 Java 代码提供元数据。作为元数据,注解不直接影响你的代码执行,但也有一些类型的注解实际上可以用于这一目的。
- 注解的本质是接口,当我们在类、方法、成员变量上使用注解的时候,相当于在此处定义了一个引用类型的变量(面向接口编程)。当使用注解时(相当于接口的实例化),通过JDK动态代理生成代理对象,而代理对象可以在不改变原有的基础上附加功能。
public @interface test {
String value();
}
通过javap -c可以得到
Compiled from "test.java"
public interface test extends java.lang.annotation.Annotation {
public abstract java.lang.String value();
}
内置注解
注解 | 作用 | 注意事项 |
---|
@Override | 它是用来描述当前方法是一个重写的方法,在编译阶段对方法进行检查 | jdk1.5中它只能描述继承中的重写,jdk1.6中它可以描述接口实现的重写,也能描述类的继承的重写 |
@Deprecated | 它是用于描述当前方法是一个过时的方法 | 无 |
@SuppressWarnings | 对程序中的警告去除 | 无 |
元注解
- 元注解顾名思义我们可以理解为注解的注解,它是作用在注解中,方便我们使用注解实现想要的功能。元注解分别有@Retention、 @Target、 @Document、 @Inherited和@Repeatable(JDK1.8加入)五种。具体参照
注解 | 作用 |
---|
@Target | 使用@Target元注解表示我们的注解作用的范围 |
@Retention | 它表示注解存在阶段是保留在源码(编译期),字节码(类加载)或者运行期(JVM中运行) |
@Documented | 它的作用是能够将注解中的元素包含到 Javadoc 中去 |
@Inherited | @Inherited注解了的注解修饰了一个父类,如果他的子类没有被其他注解修饰,则它的子类也继承了父类的注解 |
@Repeatable | 被这个元注解修饰的注解可以同时作用一个对象多次,但是每次作用注解又可以代表不同的含义 |
public class Test01 {
@MyAnnotation(name = "POP")
public void test(){
}
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
String name() default "";
int age() default 0;
int id() default -1;
String[] school() default {"A","B"};
}
通过反射获取注解
import java.lang.annotation.*;
import java.lang.reflect.Field;
public class Test02 {
public static void main(String[] args) {
try {
Class c1 = Class.forName("Student");
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
MyLabel myLabel= (MyLabel) c1.getAnnotation(MyLabel.class);
String value = myLabel.value();
System.out.println(value);
Field name = c1.getDeclaredField("name");
MyFiled name_annotation = name.getAnnotation(MyFiled.class);
System.out.println(name_annotation.columnName());
System.out.println(name_annotation.type());
System.out.println(name_annotation.length());
} catch (Exception e) {
e.printStackTrace();
}
}
}
@MyLabel("db_table")
class Student{
@MyFiled(columnName = "db_id",type = "int",length = 10)
private int id;
@MyFiled(columnName = "db_age",type = "int",length = 10)
private int age;
@MyFiled(columnName = "db_name",type = "varchar",length = 3)
private String name;
public Student(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
public Student() {
}
public int getId() {
return id;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyLabel{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyFiled{
String columnName();
String type();
int length();
}