Annotation 是一种注释,可以标注在类、方法、属性上,对其在另一个层面(一般在系统实现上)进行说明。
Annotation 的定义类似于接口:
@Retention(RetentionPolicy.RUNTIME)//指明该Annotation 在运行时存在,可通过对类反射获取标注的Annotation信息
@Target( [
ElementType.METHOD //可以标注在方法上
|ElementType.TYPE //可以标注在类上
|ElementType.FIELD //可以标注在属性上
|ElementType.CONSTRUCTOR //可以标注在构建器上
|ElementType.PARAMETER可以标注在方法参数上
]
)
[@Inherited ] //该注释的类的子类,自动具有该注释(相当于子类上写了该注释)
[public] @interface 注释名{
[类型 注释项名称A() [default 默认值];]
[类型 注释项名称B() [default 默认值];]
[类型 注释项名称C() [default 默认值];]
.....
}
举例:
定义一个实体类的注释,说明其对应数据库表的信息:
/**
* 表定义
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Table {
String tableName();//表名
String createTableSql() default "";//建表脚本
String pkFiled() default ""; //主键属性
String pkCreateType() default "";//主键生成类型
}
/**
* 数据库表中的列定义
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Column {
String columnName();//列名
String columnType();//列类型 可以再定义一下enum,表示它的可选值
}
Annotation 应用:
Annotation 对类进行注释:
@Table(tableName="person")
public class Person {
@Column(columnName="person_name",columnType="varchar2")
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
Annotation标注时写法:
@注释名(注释项A=注释值,注释项B=注释值,注释项C=注释值.....)
当一个Annotation只有一个value注释项时,标注时可以省略注释项:
@注释名(注释值)
运行时获取类的注释信息:
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
public class AnnotationUtils {
/**
* 获取类上的指定Annotation
*/
public <T extends Annotation> T getClassAnnotation(Class<?> classz,Class<T> annotationClass){
if(classz.isAnnotationPresent(annotationClass)){
return classz.getAnnotation(annotationClass);
}else{
return null;
}
}
/**
* 获取类属性上的指定Annotation
*/
public <T extends Annotation> T getFieldAnnotation(Field field,Class<T> annotationClass){
if(field.isAnnotationPresent(annotationClass)){
return field.getAnnotation(annotationClass);
}else{
return null;
}
}
/**
* 获取类方法上的指定Annotation
*/
public <T extends Annotation> T getMethodAnnotation(Method method,Class<T> annotationClass){
if(method.isAnnotationPresent(annotationClass)){
return method.getAnnotation(annotationClass);
}else{
return null;
}
}
/**
* 获取类上的所有Annotation
*/
public List<Annotation> getClassAllAnnotation(Class<?> classz ){
List<Annotation> list = null;
Annotation annotations[] = classz.getAnnotations();
if(annotations != null){
list = Arrays.asList(annotations);
}
return list;
}
/**
* 获取类属性上的所有Annotation
*/
public List<Annotation> getFieldAllAnnotation(Field field ){
List<Annotation> list = null;
Annotation annotations[] = field.getAnnotations();
if(annotations != null){
list = Arrays.asList(annotations);
}
return list;
}
/**
* 获取类方法上的所有Annotation
*/
public List<Annotation> getMethodAllAnnotation(Method method ){
List<Annotation> list = null;
Annotation annotations[] = method.getAnnotations();
if(annotations != null){
list = Arrays.asList(annotations);
}
return list;
}
public static void main(String args []){
try{
AnnotationUtils annoUtils = new AnnotationUtils();
Table table = annoUtils.getClassAnnotation(Person.class, Table.class);
if(table != null){
System.out.println("Person class -->[tableName="+table.tableName()+"]");//输出:Person class -->[tableName=person]
}
Column column = annoUtils.getFieldAnnotation(Person.class.getDeclaredField("name"),Column.class);
if(column != null){
System.out.println("Person.name -->[column name="+column.columnName()+" ,column type="+column.columnType()+"]");//输出:Person.name -->[column name=person_name ,column type=varchar2]
}
}catch(Exception e){
e.printStackTrace();
}
}
}
自定义注释的父类是:Annotation
Annotation类型的判断(源自struts2源码):
Inject findInject( Annotation[] annotations ) {
for ( Annotation annotation : annotations ) {
if (annotation.annotationType() == Inject.class) {
return Inject.class.cast(annotation);
}
}
return null;
}