Annotation自定义注解的简单使用

Annotation注解处理器

如果没有用来读取注解的方法和工作,那么注解也就不会比注释更有用处了。使用注解的过程中,很重要的一部分就是创建于使用注解处理器。Java SE5扩展了反射机制的API,以帮助程序员快速的构造自定义注解处理器。

注解处理器类库

Java使用Annotation接口来代表程序元素前面的注解,该接口是所有Annotation类型的父接口。除此之外,Java在java.lang.reflect 包下新增了AnnotatedElement接口,该接口代表程序中可以接受注解的程序元素,该接口主要有如下几个实现类:

(1) Class:类定义
(2) Constructor:构造器定义
(3) Field:累的成员变量定义
(4) Method:类的方法定义
(5) Package:类的包定义

java.lang.reflect 包下主要包含一些实现反射功能的工具类,实际上,java.lang.reflect 包所有提供的反射API扩充了读取运行时Annotation信息的能力。当一个Annotation类型被定义为运行时的Annotation后,该注解才能是运行时可见,当class文件被装载时被保存在class文件中的Annotation才会被虚拟机读取。

AnnotatedElement 接口是所有程序元素(Class、Method和Constructor)的父接口,所以程序通过反射获取了某个类的AnnotatedElement对象之后,程序就可以调用该对象的如下四个个方法来访问Annotation信息:

方法1: T getAnnotation(Class annotationClass): 返回改程序元素上存在的、指定类型的注解,如果该类型注解不存在,则返回null。

方法2:Annotation[] getAnnotations():返回该程序元素上存在的所有注解。

方法3:boolean is AnnotationPresent(Class<?extends Annotation> annotationClass):判断该程序元素上是否包含指定类型的注解,存在则返回true,否则返回false。

方法4:Annotation[] getDeclaredAnnotations():返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。(如果没有注释直接存在于此元素上,则返回长度为零的一个数组。)该方法的调用者可以随意修改返回的数组;这不会对其他调用者返回的数组产生任何影响。

一个简单的注解处理器:

水果名称注解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package annotation;
import java.lang.annotation.*;
/** 水果名称注解
* Created by zhouxu on 2017/11/22 16:18.
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitName {
// 该注解用于修饰成员变量
public String value() default "";
}

水果颜色注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package annotation;
import java.lang.annotation.*;
/** 水果颜色注解
* Created by zhouxu on 2017/11/22 16:23.
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitColor {
/**
* 颜色枚举
*/
public enum Color{BULE,RED,GREEN};
/** 颜色属性
* @return
*/
public Color fruitColor() default Color.BULE;
}

水果提供者注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package annotation;
import java.lang.annotation.*;
/**
* 水果提供者注解
* Created by zhouxu on 2017/11/22 16:42.
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitProvider {
/** 供应商编号
* @return
*/
public int id() default -1;
/** 供应商名称
* @return
*/
public String name() default "";
/** 供应商地址
* @return
*/
public String address() default "";
}

在JavaBean中的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package annotation;
/**
* Created by zhouxu on 2017/11/22 16:55.
*/
public class Apple {
@FruitName("Apple")
private String appleName;
@FruitColor(fruitColor = FruitColor.Color.RED)
private String appleColor;
@FruitProvider(id = 1, name = "上海红富士集团", address = "上海市徐汇区古美路凤凰大厦")
private String appleProvider;
public Apple() {
}
public Apple(String appleName, String appleColor, String appleProvider) {
this.appleName = appleName;
this.appleColor = appleColor;
this.appleProvider = appleProvider;
}
public String getAppleName() {
return appleName;
}
public void setAppleName(String appleName) {
this.appleName = appleName;
}
public String getAppleColor() {
return appleColor;
}
public void setAppleColor(String appleColor) {
this.appleColor = appleColor;
}
public String getAppleProvider() {
return appleProvider;
}
public void setAppleProvider(String appleProvider) {
this.appleProvider = appleProvider;
}
@Override
public String toString() {
return "Apple{" +
"appleName='" + appleName + '\'' +
", appleColor='" + appleColor + '\'' +
", appleProvider='" + appleProvider + '\'' +
'}';
}
}

注解处理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package annotation;
import org.apache.log4j.Logger;
import annotation.FruitColor.Color;
import java.lang.reflect.Field;
/**
* 注解处理器
* Created by zhouxu on 2017/11/22 17:01.
*/
public class FruitInfo {
private static Logger log = Logger.getLogger(FruitInfo.class);
private static void getFruitInfo(Class<?> clazz) {
// 获取所有的成员变量包括私有属性,返回数组(getDeclaredFields()方法可以获取所有的成员变量)
// 通过反射机制获取类的私有成员变量
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
log.info("field.getName()>>>>>" + field.getName());
log.info("log.info(field.getAnnotatedType())>>>>>" + field.getAnnotatedType());
log.info("field.getAnnotations()>>>>>" + field.getAnnotations());
log.info("field.getDeclaredAnnotations()" + field.getDeclaredAnnotations());
log.info("fields>>>>>" + field);
}
for (Field field : fields) {
if (field.isAnnotationPresent(FruitName.class)) {
FruitName fruitName = field.getAnnotation(FruitName.class);
String value = fruitName.value();
log.info("水果名称>>>>>" + value);
} else if (field.isAnnotationPresent(FruitColor.class)) {
FruitColor fruitColor = field.getAnnotation(FruitColor.class);
Color color = fruitColor.fruitColor();
log.info("水果颜色>>>>>" + color.toString());
} else if (field.isAnnotationPresent(FruitProvider.class)) {
FruitProvider fruitProvider = field.getAnnotation(FruitProvider.class);
int id = fruitProvider.id();
String name = fruitProvider.name();
String address = fruitProvider.address();
log.info("供应商编号>>>>>" + id + " 供应商名称>>>>>" + name + " 供应商地址>>>>>" + address);
}
}
}
public static void main(String[] args) {
FruitInfo.getFruitInfo(Apple.class);
}
}

输出结果

1
2
3
4
5
6
7
8
2017-12-01 16:14:38,308 [main] INFO [annotation.FruitInfo] - field.getName()>>>appleName
2017-12-01 16:14:38,308 [main] INFO [annotation.FruitInfo] - field.getName()>>>appleColor
2017-12-01 16:14:38,308 [main] INFO [annotation.FruitInfo] - field.getName()>>>appleProvider
2017-12-01 16:14:38,324 [main] INFO [annotation.FruitInfo] - 水果名称>>>Apple
2017-12-01 16:14:38,340 [main] INFO [annotation.FruitInfo] - 水果颜色>>>RED
2017-12-01 16:14:38,340 [main] INFO [annotation.FruitInfo] - 供应商编号>>>1 供应商名称>>>上海红富士集团 供应商地址>>>上海市徐汇区古美路凤凰大厦
Process finished with exit code 0

分享

Powered by Hexo and Hexo-theme-hiker

Copyright © 2018 - 2019 ZhouXu'Blog All Rights Reserved.

UV : | PV :