Spring注解

深入理解spring注解的构成

自定义注解

1
2
3
4
5

public @interface zhujie {

}

其实最简单注解就是这样的,直接使用@interface后面跟上你想自定义的注解的名字即可

当然了,有一些本身在注解上常用的注解,下面一一来介绍。

原生注解

@target

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

@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();

}

elementpye

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

public enum ElementType {

/** Class, interface (including annotation type), or enum declaration */

//类,接口或者枚举类

TYPE,



/** Field declaration (includes enum constants) */

//变量声明

FIELD,



/** Method declaration */

//方法声明

METHOD,



/** Formal parameter declaration */

//格式化声明

PARAMETER,



/** Constructor declaration */

//构造声明

CONSTRUCTOR,



/** Local variable declaration */

//本地变量声明

LOCAL_VARIABLE,



/** Annotation type declaration */

//注解类型声明

ANNOTATION_TYPE,



/** Package declaration */

//打包

PACKAGE,



/**

* Type parameter declaration

*

* @since 1.8

*/

TYPE_PARAMETER,



/**

* Use of a type

*

* @since 1.8

*/

//用户类型

TYPE_USE

}

@Retention

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.ANNOTATION_TYPE)

public @interface Retention {

/**

* Returns the retention policy.

* @return the retention policy

*/

RetentionPolicy value();

}

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

public enum RetentionPolicy {

/**

* Annotations are to be discarded by the compiler.

* 批注将被编译器丢弃

*/

SOURCE,



/**

* Annotations are to be recorded in the class file by the compiler

* but need not be retained by the VM at run time. This is the default

* behavior.

*注释将由编译器记录在类文件中,但VM不必在运行时保留它们。 这是默认行为。

*/

//

CLASS,



/**

* Annotations are to be recorded in the class file by the compiler and

* retained by the VM at run time, so they may be read reflectively.

*注释将由编译器记录在类文件中,并在运行时由VM保留,因此可以通过反射方式读取它们。

* @see java.lang.reflect.AnnotatedElement

*/

RUNTIME

}

@Documented

1
2
3
4
5
6
7
8
9
10
11

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.ANNOTATION_TYPE)

public @interface Documented {

}