Java注解又称为标注,是Java从1.5开始支持的特殊语法元数据;Java中的类、方法、变量、参数、包都可以被注解。
元数据:描述数据的数据,对数据及信息资源的描述性信息。
1、注解仅仅是元数据,和业务逻辑无关,所以当你查看注解类时,发现里面没有任何逻辑处理;
2、javadoc中的@author、@version、@param、@return、@deprecated、@hide、@throws、@exception、@see是标记,并不是注解。
注解的作用:
1、在编译时进行格式检查:如果某个方法签名使用了@override,而它的父类里面并没有这个方法,此时编译器就会报错。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
2、减少工作量:大量重复的工作可以通过注解来减少工作量。
缺点:
1、业务类之间的关系不如XML配置那样一目了然
2、扩展性较差
Java SE5内置了三种,定义在java.lang中的注解:
1、@Override,表示当前的方法定义将覆盖超类中的方法;
2、@Deprecated,告知用户和编译器被标记的内容已不建议被使用,如果被使用,编译器会报警告,但不会报错,程序也能正常运行;
3、@SuppressWarnings,关闭不当的编译器警告信息
元注解
Java目前内置了三种标准注解(上面的),以及四种元注解,元注解专职负责注解 其他的注解。
@Target
|
表示该注解可以用于什么地方。可能的ElementType参数包括:
CONSTRUCTOR:构造器声明
FIELD:域声明(包括enum实例)
LOCAL_VARIABLE:局部变量声明
METHOD:方法声明
PACKAGE:包声明
PARAMETER:参数声明
TYPE:类、接口(包括注解类型)或enum声明
(没指定的话可以放到任意地方)
|
@Retention
|
表示需要在什么级别保存该注解信息。参数有:
SOURCE:注解将被编译器丢弃(如:@Override、@SuppressWarnings)
CLASS:注解在class文件中可用,但会在VM中丢弃(默认值;如:@Deprecated)
RUNTIME:VM将在运行期也保留注解,因此可以通过反射机制读取注解信息 |
@Documented
|
将此注解包含在Javadoc中(表示是否出现在API文档中)
|
@Inherited
|
允许子类继承父类中的注解
|
自定义注解
注解的特征:
使用关键字@interface ,注解不支持继承,不能使用extends来继承某个@interface。
注解类顶部会被@Documented、@Retention、@Tartget、@Inherited这四个元注解标记
其中@Tartget必须要有,其他可选
任何注解都离不开元注解(包括元注解自身如下@Target注解的代码,通过元注解可以自定义注解)。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to
*/
ElementType[] value();
}
使用@interface自定义注解时,就会自动继承java.lang.annotation.Annotation接口,同时不能继承其他的注解或接口。
Annotaion中的成员变量以方法的形式来定义,方法的名称就是参数的名称,返回值类型就是参数的类型(返回值类型只能是基本类型、Class、String、enum)。可以通过default来声明参数的默认值。
注解元素可用的类型如下:
1、所有基本数据类型(int、float、boolean等)
2、String
3、Class
4、enum
5、Annotation
6、以上类型的数组
如果使用了其他数据类型,编译器就会报错。(以上摘自《Java编程思想第4版》)
自定义注解的格式:
元注解
public @interface 注解名{
定义体;
}
定义注解只能用public或者默认(default)这两个访问权修饰,例如String value();这里把方法设为default默认类型:
package com.fxp.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyFirstAnnotation {
String value() default "";
}
注意:1、参数的默认值不能为null;
2、注解只有一个参数的时候,参数名必须取为value,且使用时可以忽略参数名和“=”。
注解元素必须有确定的值;要么指定时给默认值,要么使用时给值。不过有时候我们需要确定表达一个元素不存在值,所以使用空字符串或者负数表示某个元素不存在,在定义注解时,这已经成为一个约定用法。
自定义注解示例:
事先准备一个Person接口和Child实现类
package com.fxp.annotation.test;
public interface Person {
String name();
int age();
}
package com.fxp.annotation.test;
public class Child implements Person {
@Override
public String name() {
// TODO Auto-generated method stub
return null;
}
@Override
public int age() {
// TODO Auto-generated method stub
return 0;
}
}
定义一个注解类Description:
package com.fxp.annotation.test;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.TYPE})//作用域:在类和方法上可用
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
String value();
}
在Child类上使用该注解:
package com.fxp.annotation.test;
@Description("I am Child Class annotation")
public class Child implements Person {
@Override
@Description("I am method annotation")
public String name() {
// TODO Auto-generated method stub
return null;
}
@Override
public int age() {
// TODO Auto-generated method stub
return 0;
}
}
定义一个解析注解的类:
package com.fxp.annotation.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class ParseAnno {
public static void main(String[] args) {
try {
//1、使用类加载器加载类
Class c = Class.forName("com.fxp.annotation.test.Child");
//2、找到类上面的注解
boolean isExist = c.isAnnotationPresent(Description.class);
if(isExist){//判断注解是否存在
//3、拿到注解实例
Description d = (Description) c.getAnnotation(Description.class);
System.out.println(d.value());
}
//4、找到方法上的注解
Method[] ms = c.getMethods();
for(Method m : ms){
boolean isMExist = m.isAnnotationPresent(Description.class);
if(isMExist){
Description d = m.getAnnotation(Description.class);
System.out.println(d.value());
}
}
//另外一种解析方法
for(Method m : ms){
Annotation[] as = m.getAnnotations();
for(Annotation a : as){
if(a instanceof Description){
System.out.println(((Description) a).value());
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
解析的结果:
I am Child Class annotation
I am method annotation
I am method annotation