In many Java code, you can see characters such as @override, @Deprecated, @SuppressWarnings, which are annotation annotation. Annotations were first introduced in Jdk5 and have now become an important part of the Java platform, and many of the framework programs also like to use annotations, such as spring, mybatis, etc.
So, what is an annotation? Annotations are metadata, a kind of data that describes data, and it is common to add more visual explanations to the elements (classes, methods, member variables) of the program, which are independent of the business logic of the program. However, we can use the reflection mechanism of Java to obtain annotation information, and according to this information to the program assignment, distribution and other operations. java5.0 defines 4 standard meta-annotation-meta annotations, which are used to provide descriptions of other annotation types, and four types of meta annotations are as follows:
- @Target;
- @Retention;
- @Inherited;
- @Documented;
The functions of these four meta-annotations are explained in detail below: @Target is used to describe the use of annotations, that is, where annotations can be used in a decorated object, the value can be one of the ElementType:
- CONSTRUCTOR: Used to describe the constructor;
- Field: Used to describe the domain;
- Local_variable: Used to describe local variables;
- Method: Used to describe methods;
- Package: Used to describe packages;
- PARAMETER: Used to describe parameters;
- Type: Used to describe a class, interface, annotation type, or enumeration;
@Target (Elementtype.type) Public@interface exculde{/** * Name, default value is "" * @return*/ PublicString name ()default "";} @Target (Elementtype.field) Public@interface inject{/** * ID, default value is "" * @return*/ PublicString ID ()default ""; /** class, the default value is "" * @return*/ PublicClass Clazz ()defaultObject.class; }There are two annotations defined above, annotated @exculde can only be used to decorate classes, interfaces, annotations, enumerations, annotations @inject only fields of a type, such as:
@Exculde (name="admin")publicclass user{ @Inject ( ID="username", clazz=string. class ) private String username;}
@Retention is used to describe the life cycle of annotations, that is, the annotations can be valid at which level of the source to the JVM loading process. Some annotation only appear in the source code, is discarded by the compiler, some annotation can be compiled into the class file, may be ignored by the JVM, some annotation not only can be compiled into the class file, and can be read when the class file is loaded. These three cases correspond to the three values of Retentionpoicy:
- Source: The source file is retained;
- Class:class file;
- Runtime: run-time retention;
@Inherited is used to describe annotations that can be inherited, and if a annotation that uses the @inherited modifier is used for a class, the annotation is also used for subclasses of this class. @Inherited is a markup annotation, there is no parameter option, the annotation that it modifies is inherited by a subclass of the tagged class, and the class does not inherit annotation from the interface it implements, and the method does not inherit annotation from the method it overloads. When using Java reflection to get a @inherited decorated annotation, the reflection check recursively checks the class and its parent class until it finds that the specified annotation type is found, or reach the top level of the class inheritance structure. @Documented The public API used to describe the annotation information should be used as the annotated program, i.e. the annotation information should be kept in the document. @Documented is also a markup annotation with no parameter options.
@Target (Elementtype.type) @Retention (retentionpolicy.runtime) @Inherited @documented Public @interface exculde{ * * * name annotation, default value is "" * @return */ publicdefault"";}
If you've seen the meta-annotations above, it doesn't matter if you don't understand them, let's use custom annotations to further understand Meta annotations. Custom annotations require the use of @interface, similar to defining a class using class, but no longer inherit other classes or interfaces when defining annotations, which automatically inherits the Java.lang.annotation.Annotation interface. @interface is used to declare an annotation in which each method actually declares a configuration parameter, the name of the method is the name of the parameter, the type of the return value of the method is the type of the parameter, or the default value of the parameter can be declared using default. The format for defining annotations is as follows: public @interface the annotation name {definition body} annotation parameter can support the following data types:
- Basic type;
- Class;
- String;
- Enum;
- Annotation;
- The array form of all the above types;
The following defines a spring-like annotation that is used to inject the value of a property into an instance object:
@Target (Elementtype.field) @Retention (retentionpolicy.runtime) Public @interface inject{ /* * ID, the default value is "" * @return * * publicdefault""; /* * class, the default value is "" * @return */public default Object. Class;}
Label the @inject on the properties of the class Useraction:
Public class useraction { @Inject (id="userservice", Clazz=userservice. Class) private userservice userservice;}
In the IOC container frame, the object is automatically initialized, and if we want to implement this function of the IOC, we should inject its value for the attribute userservice with the @inject annotation. First we should get UserService domain object field by reflection, get the information of @inject annotation through field, then get the value it depends on based on the ID and clazz of the annotations:
= Field.getannotation (Inject. Class== = Class.forName (clazz.getname). newinstance (); Field.setaccessible ( true); field. Set (object, UserService);
In the above code, call Field.getannotation (Inject.class) to get the object to @inject, then get the @inject ID and clazz value, instantiate the Clazz object by reflection, and then reflect the value to field. This is the implementation principle of spring's dependency injection of those frameworks, and it is interesting to be able to optimize it yourself. Read the annotation information of the class there are several other methods, where no longer one by one, you can study the Java.lang.reflect package. Through the above instructions, we can also know that annotations are just a kind of metadata, enhance the description of classes, attributes, parameters, the key to using annotations is to obtain the information of the annotations, and then through the means of reflection to achieve the function of annotations. Article sync published in Lang degree cloud website, Portal: http://www.wolfbe.com/detail/201608/265.html
Java Basics-Annotations annotation principles and usage