Java annotations
Annotation (note) was introduced in JDK5.0 and later versions. It can be used to create documents, track dependencies in code, and even perform basic compile-time checks. Annotations are present in the code with the ' @ annotation name ', and according to the number of annotation parameters, we can divide the annotations into three categories: tag annotation, single value annotation, complete annotation. None of them directly affects the semantics of the program, just as annotations (identities) exist, and we can programmatically implement the access to those metadata (the data used to describe the data) through the reflection mechanism. In addition, you can choose whether the annotations in your code exist at the source level at compile time, or it can appear in the class file.
Annotations that are commonly encountered in writing code are @override, @SuppressWarnings, @Override annotations that override the parent class method, @SuppressWarnings annotations that ignore the warning message
About the four effects of the same annotation
@Component ("info") not layered
@Repository ("info") DAO
@Service ("info") Biz
@Controller ("info") action
@Value () inject normal property values in a class
@Resource to inject domain properties into a class
@Autowired to inject domain properties into a class
Annotations (Annotation) Getting Started with custom annotations
The role of meta annotations is to be responsible for annotating other annotations. Java5.0 defines 4 standard meta-annotation types, which are used to provide descriptions of other annotation types. Java5.0 defined meta-annotations:
[Email protected],
[Email protected],
[Email protected],
[Email protected]
These types and the classes they support can be found in the Java.lang.annotation package. Let's take a look at the function of each meta-annotation and the instructions for using the corresponding sub-parameters.
@Target:
@Target illustrates the range of objects that annotation modifies: annotation can be used for packages, types (classes, interfaces, enumerations, annotation types), type members (methods, constructor methods, member variables, enumeration values), Method parameters and local variables (such as loop variables, catch parameters). Target can be more clearly decorated by using target in the declaration of the annotation type.
Function: Used to describe the scope of use of annotations (i.e., where the annotations described can be used)
The values (ElementType) are:
1.CONSTRUCTOR: Used to describe the constructor
2.FIELD: Used to describe the domain
3.local_variable: Used to describe local variables
4.METHOD: Used to describe the method
5.PACKAGE: Used to describe the package
6.PARAMETER: Used to describe parameters
7.TYPE: Used to describe classes, interfaces (including annotation types) or enum declarations
When you use @interface to customize annotations, the Java.lang.annotation.Annotation interface is automatically inherited and other details are automatically completed by the compiler. When you define annotations, you cannot inherit other annotations or interfaces. @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, and the return value type is the type of the parameter (the return value type can only be base type, Class, String, enum). You can declare the default value of a parameter through default.
To define the annotation format:
Public @interface annotation name {definition body}
Supported data types for annotation parameters:
1. All basic data Types (Int,float,boolean,byte,double,char,long,short)
2.String type
3.Class type
4.enum type
5.Annotation type
6. Arrays of all types above
Custom Annotation Cases:
1. Custom annotations
1 @Target (Elementtype.method)2 @Retention (retentionpolicy.runtime)3 @Documented4 Public@InterfaceApple {5 String name ();6String Sex ()defaultMale;7String Phone ()default"15931787945";8 intAge ()default2;9}
2. Classes that use annotations:
1 Public class appletest {2 @Apple (name= "Zhang Duo")3public void Show () {45 }6 }
3. Extract the annotation parameters:
1 Public classtest20171010{2 @Test3 Public voidtest01 () {4Class<appletest> Appletestclass = appletest.class;5 Try {6Apple show = Appletestclass.getmethod ("show"). Getannotation (Apple.class);7 System.out.println (Show.name ());8 System.out.println (Show.sex ());9 System.out.println (Show.phone ());Ten System.out.println (Show.age ()); One}Catch(nosuchmethodexception e) { A e.printstacktrace (); - } - } the}
Execution Result:
Java Custom Annotations