
Java annotation
Preface
Recently, I was reading open source projects , Found a lot of strange notes in the project (@DataScope
、@Log
...) I look confused , I don't know if you've had this kind of experience , Think about , Discover your knowledge of annotations , It seems that only stay in @Override
... It's awkwardness , So I'll make up for it today annotation This knowledge , And record your harvest here , Communicate with you , If there is something wrong , Please correct me !
I hope this paper can bring readers the following harvest :
- Understand what annotations are , What's the use
- Understand the role of annotations in other people's code
- Can use custom annotations
What is annotation
Want to know something , The first thing I recommend is to check the official website , Let's see below. Java The official interpretation of the note :
Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.
Annotations are a form of metadata , It provides data about the program , But the data is not part of the program itself . Annotations have no direct effect on the operation of the code they annotate .
A bunch of English , A cloud of mist . No problem , This is normal operation , However, we can still learn from translation that annotations can provide data , And the data is program independent , So we can roughly infer that , Annotation is actually a medium between program and data , Programs and data make a connection through annotations , namely annotation It's like a red line , Put data and programs relation together .
from @Override
Start
Through to Java Official translation of explanatory notes , We filtered out a key message —— relation . So how to understand the word ? Don't worry. , From the most familiar strangers @Override
Start , Most familiar because we know that this is method rewriting , Subclasses override annotations used by parent methods , It's strange because we've never been able to click in to understand this annotation , Then go in and have a look !
import java.lang.annotation.*;@Target(ElementType.METHOD)@Retention(RetentionPolicy.SOURCE)public @interface Override {}
Short 5 That's ok , Except for the first line , I don't know anything else ... Don't worry , Let's read it line by line !
- The annotation imports a
annotation
package - Annotated “ Dolls ” Behavior
@Target(ElementType.METHOD)
、@Retention(RetentionPolicy.SOURCE)
- Different from the declaration of interfaces and classes
public @interface Override { }
Except for the new annotations , We can roughly understand the definition format of annotations , Modifier @interface Annotation name {}
.( It's a bit of an interface )
No doll —— Yuan notes
Through to @Override
Analysis of , We learned about the definition format of annotations , But we found that there was a new annotation in the annotation , In the spirit of inquisitive curiosity , We continue to enter @Target
A note to find out !
@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Target { ElementType[] value();}
Keep clicking the , The discovery is always in @Documented
、@Retention
、@Target
Between these notes , adopt Java In the document, we learned that these annotations that modify annotations are called meta annotations . Yuan notes (meta-annotation) stay java.lang.annotation
It's a bag :
@Retention
Indicates how to store tagged annotations ( Specify the storage level ), There are three levels
- RetentionPolicy.SOURCE: Only to the source level , It will be ignored in the compilation phase , So they're not written into bytecode .
- RetentionPolicy.CLASS:( Default ) Compilation level , Reserved by the compiler at compile time , But being Java virtual machine (JVM) Ignore .
- RetentionPolicy.RUNTIME: from JVM Retain , You can use... In the runtime environment .
@Target
Indicates what kind of annotation marked can be used for java Elements ( class 、 Interface 、 attribute 、 Method ......), There are eight kinds of
Scope | explain |
---|---|
ElementType.ANNOTATION_TYPE | Can be used for annotation types |
ElementType.CONSTRUCTOR | Can be used for constructors |
ElementType.FIELD | Can be used for fields or properties |
ElementType.LOCAL_VARIABLE | Can be used for local variables |
ElementType.METHOD | Can be used for method level annotations |
ElementType.PACKAGE | Can be used for package declaration |
ElementType.PARAMETER | Parameters that can be used for methods |
ElementType.TYPE | Can be used for any element of a class |
@Documented
Whenever the specified annotation is used , Should be used Javadoc Tools record these elements .( It's going to be generated javadoc Add a note to explain )
@Inherited
Annotation types can be inherited from superclasses , For class declaration only ( Interfaces do not inherit )
@Repeatable
stay Java SE 8 Introduced in , Comments that represent tags can be applied to the same declaration or type use more than once .
Classification of annotations
By understanding meta annotations , I've learned that an annotation is modified by these meta annotations , And we've got an important message —— Annotations can modify annotations
So infinite Dolls , There will be all kinds of annotations , So what are the annotations ? Common annotations can be roughly divided into the following four categories :
Yuan notes
As mentioned above 5 A meta note
jdk annotation
The common ones are @Override
@Deprecated
@SuppressWarnings
@SafeVarargs
@FunctionalInterface
Third party comments
That is, annotations provided by third-party frameworks , For example, automatic injection of dependencies @Autowired
、@Controller
etc.
Custom annotation
That is, developers can customize annotations according to project requirements , For some tools in compilation 、 Runtime for parsing and use , Play a show 、 Configured functionality .
actual combat —— Define your own annotations
Yes Java Notes provided , I believe you have a general understanding of annotations . Have you ever thought about , Annotation is how to turn corruption into magic , Added a simple @Autowired
We can implement dependency injection 、@Setter
Can achieve set Method generation , Let's experience the magic of annotation through simple actual combat !
Actual target :
Use custom annotations , By annotating entity classes and their properties , Realize the query of entity class sql The construction of statements
ps: similar
select * from t_user where t_name='kingwan'
In the form of
Custom annotation writing rules
Before the actual battle , Let's take a look at writing custom annotations The rules :
- Annotation is defined as
@interface
, All annotations are automatically inheritedjava.lang.Annotation
This interface , And you can't inherit other classes or interfaces - Parameter members can only use
public
ordefault( Default )
Access modifiers - Parameters .........