Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

In-depth understanding of Java: introduction to custom annotations (Annotation)

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

To learn more about annotations, we must be able to define and use our own annotations, and before defining our own annotations, we must understand the syntax of meta-annotations and related definition annotations provided to us by Java.

Meta-comments:

The role of meta-annotations is to be responsible for annotating other notes. Java5.0 defines four standard meta-annotation types that are used to provide descriptions of other annotation types. Meta-annotations for Java5.0 definitions:

1.@Target

2.@Retention

3.@Documented

4.@Inherited

These types and the classes they support can be found in the java.lang.annotation package. Let's take a look at the role of each meta-annotation and the instructions for the use of the corresponding sub-parameters.

@ Target:

@ Target illustrates the scope of objects that Annotation modifies: Annotation can be used for packages, types (classes, interfaces, enumerations, Annotation types), type members (methods, constructors, member variables, enumerated values), method parameters, and local variables (such as loop variables, catch parameters). The use of target in the declaration of the Annotation type makes the goal of its embellishment clearer.

Function: used to describe the scope of use of annotations (that is, where the annotations described can be used)

The values (ElementType) are:

1.CONSTRUCTOR: used to describe constructors

2.FIELD: used to describe the domain

3.LOCAL_VARIABLE: used to describe local variables

4.METHOD: used to describe methods

5.PACKAGE: used to describe packages

6.PARAMETER: used to describe parameters

7.TYPE: used to describe classes, interfaces (including annotation types), or enum declarations

Examples of use:

@ Target (ElementType.TYPE) public @ interface Table {/ * Datasheet name annotation, default is class name * @ return * / public String tableName () default "className";} @ Target (ElementType.FIELD) public @ interface NoDBColumn {}

Annotated Table can be used for annotated classes, interfaces (including annotation types), or enum declarations, while annotated NoDBColumn can only be used for member variables of annotated classes.

@ Retention:

@ Retention defines how long the Annotation is retained: some Annotation appears only in the source code and is discarded by the compiler, while others are compiled in the class file; the Annotation compiled in the class file may be ignored by the virtual machine, while others will be read when the class is loaded (note that it does not affect the execution of the class, because Annotation and class are separated in use). Using this meta-Annotation, you can limit the "lifecycle" of Annotation.

Purpose: indicates at what level the annotation information needs to be saved to describe the lifecycle of the annotation (that is, to what extent the described annotation is valid)

The values (RetentionPoicy) are:

1.SOURCE: valid in the source file (that is, source file retention)

2.CLASS: valid in class files (that is, class retention)

3.RUNTIME: valid at runtime (that is, retained at runtime)

The Retention meta-annotation type has a unique value as a member, and its value comes from the enumerated type value of java.lang.annotation.RetentionPolicy. Specific examples are as follows:

@ Target (ElementType.FIELD) @ Retention (RetentionPolicy.RUNTIME) public @ interface Column {public String name () default "fieldName"; public String setFuncName () default "setField"; public String getFuncName () default "getField"; public boolean defaultDBValue () default false;}

The attribute value of the RetentionPolicy of the Column annotation is RUTIME, so that the annotation processor can obtain the attribute value of the annotation through reflection, thus doing some runtime logic processing.

@ Documented:

Documented is used to describe other types of annotation that should be used as the public API of annotated program members, so they can be documented by tools such as javadoc. Documented is a tag comment and has no members.

@ Target (ElementType.FIELD) @ Retention (RetentionPolicy.RUNTIME) @ Documentedpublic @ interface Column {public String name () default "fieldName"; public String setFuncName () default "setField"; public String getFuncName () default "getField"; public boolean defaultDBValue () default false;}

@ Inherited:

The @ Inherited meta-annotation is a tag annotation, and @ Inherited states that a certain annotated type is inherited. If an annotation type with the @ Inherited modifier is used for a class, the annotation will be used for a subclass of that class.

Note: the @ Inherited annotation type is inherited by a subclass of the annotated class. The class does not inherit annotation from the interface it implements, and the method does not inherit annotation from the method it overloads.

When the Retention of the annotation annotated by the @ Inherited annotation type is RetentionPolicy.RUNTIME, the reflected API enhances this inheritance. If we use java.lang.reflect to query an annotation of type @ Inherited annotation, reflection code checking will work: check the class and its parent class until it is found that the specified annotation type is found, or reaches the top level of the class inheritance structure.

Example code:

/ * @ author peida * * / @ Inheritedpublic @ interface Greeting {public enum FontColor {BULE,RED,GREEN}; String name (); FontColor fontColor () default FontColor.GREEN;}

Custom comments:

When using @ interface custom annotations, the java.lang.annotation.Annotation interface is automatically inherited, and other details are automatically completed by the compiler. When defining annotations, you cannot inherit other annotations or interfaces. @ interface is used to declare an annotation, where 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 the base type, Class, String, enum). You can declare the default value of the parameter through default.

Define the annotation format:

Public @ interface Annotation name {definition body}

Supported data types of annotation parameters:

1. All basic data types (int,float,boolean,byte,double,char,long,short)

2.String Typ

3.Class Typ

4.enum Typ

5.Annotation Typ

6. Arrays of all the above types

How to set the parameters in the Annotation type:

First, you can only use public or default (default) these two access rights. For example, String value (); here set the method to the default type of defaul;

Second, parameter members can only use basic types byte,short,char,int,long,float,double, eight basic Boolean data types and String,Enum,Class,annotations data types, as well as arrays of these types. For example, String value (); here the parameter member is String;

Third, if there is only one parameter member, it is best to set the parameter name to "value" followed by parentheses. Example: the following example FruitName annotation has only one parameter member.

Get [download address]

20. System settings: modify system name, mail server configuration, SMS account settings, picture watermark configuration, Wechat configuration

21. Chat in time: open the chat window, group chat, one-on-one chat

twenty-two。 Form construction: drag-and-drop quick custom build forms with rich building elements, rich text, upload controls, drop-down boxes, etc.

-Database management

23. Database backup: can back up single table, entire database, support local and remote database backup (java interface programming technology, socket programming technology)

24. Backup timer: quartz 2.2 powerful task scheduling, multi-thread backup database, task startup and shutdown asynchronous operation

25. Database restore: historical backup records, restore database or single table, statistics backup time and file size

twenty-six。 SQL Editor: a powerful SQL editor that supports editing complex query statements, generating dynamic reports, and exporting excel

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report