1 概述
1.1 为什么要使用注解
注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5是注解+配置文件的方式,spring4以后注解为主,springboot以后全是注解,注解是一种趋势,现在已经有不少的人开始用注解了。
1.2 注解说明
Java注释很类似
Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。
Java 语言中的类、方法、变量、参数和包等都可以被标注。和 Javadoc 不同,Java 标注可以通过反射获取标注内容。在编译器生成类文件时,标注可以被嵌入到字节码中。Java 虚拟机可以保留标注内容,在运行时可以获取到标注内容 。
当然它也支持自定义 Java 标注。
注解和注释类似的作用,注释用于帮助程序员更好的理解代码的意义,而注解是告诉编译器此代码的特殊含义.
1.3 JAVA内置注解
Java 定义了一套注解,共有 7 个,3 个在 java.lang 中,剩下 4 个在 java.lang.annotation 中。
作用在代码的注解是
@Override - 检查该方法是否是重写方法。如果发现其父类,或者是引用的接口中并没有该方法时,会报编译错误。
@Deprecated - 标记过时方法。如果使用该方法,会报编译警告。
@SuppressWarnings - 指示编译器去忽略注解中声明的警告。
作用在其他注解的注解(或者说 元注解)是:
@Retention - 标识这个注解怎么保存,是只在代码中,还是编入class文件中,或者是在运行时可以通过反射访问。
@Documented - 标记这些注解是否包含在用户文档中。
@Target - 标记这个注解应该是哪种 Java 成员。
@Inherited - 标记这个注解是继承于哪个注解类(默认 注解并没有继承于任何子类)
注解实例
package com.xja.ssm.jingpai.bean;
import com.xja.ssm.jingpai.Annotation.MyAnnMethod;
import com.xja.ssm.jingpai.Annotation.MyAnnOfField;
import com.xja.ssm.jingpai.Annotation.MyAnnOfType;
@MyAnnOfType
public class StudentTest {
@MyAnnOfField(value="lingnan")
public String name;
@MyAnnOfField
public String sex;
public String classes;
public void show() {
System.out.println("姓名:"+this.name+",性别:"+this.sex+",班级:"+this.classes);
}
@MyAnnMethod(value="method")
public void showMyInfo() {
System.out.println("姓名:"+this.name+",性别:"+this.sex+",班级:"+this.classes);
}
}
package com.xja.ssm.jingpai.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.xja.ssm.jingpai.Annotation.MyAnnOfField;
import com.xja.ssm.jingpai.bean.StudentTest;
public class AnnotationTest {
public static void main(String[] args) {
//直接使用对象去给属性赋值,调用方法,那么这些注解都没有作用
/* StudentTest stu = new StudentTest();
stu.name="jingyi";
stu.sex="男";
stu.classes="一班";
stu.showMyInfo();*/
try {
Class student = Class.forName("com.ssm.jingpai.bean.StudentTest");
//创建StudentTest的对象
Object o=student.newInstance();
//获取类上的所有注解信息
Annotation[] TypeAnnotations = student.getDeclaredAnnotations();
System.out.println("类上的注解信息为:");
for (Annotation annotation : TypeAnnotations) {
System.out.println(annotation);
}
//获取所有字段
Field[] Fields = student.getDeclaredFields();
System.out.println("字段上的注解信息为:");
Boolean flag=false;
for (Field field : Fields) {
Annotation[] fieldsAnnotations = field.getDeclaredAnnotations();
for (Annotation annotation : fieldsAnnotations) {
System.out.println(annotation.toString());
if(annotation instanceof MyAnnOfField) {
MyAnnOfField myAnn=(MyAnnOfField)annotation;
if(myAnn.value().equals("myField")) {
field.set(o, "默认值");
}else if(myAnn.value().equals("lingnan")){
field.set(o, "凌楠");
}else {
field.set(o, "未经过注解");
}
flag=true;
}
if(flag==false) {
field.set(o,"未使用注解");
}
}
Method show=student.getDeclaredMethod("showMyInfo");
show.invoke(o, null);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}