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

How to use annotations in Java

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

Share

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

This article introduces the relevant knowledge of "how to use annotations in Java". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, let's take a look at the concept. First of all, from the annotation point of view.

Comments: add notes and explanations to the code, and comments help developers understand the program. (Comment) to put it bluntly, notes are for people to read.

Note: add an explanation to the code, which is used by the program. (Annotation)

Since JDK 5. 0, Java has added support for MetaData, or Annotation.

Three basic Annotation:

@ Override: limit overriding parent methods. This annotation can only be used for methods.

@ Deprecated: used to indicate that a program element (class, method, etc.) is out of date

SuppressWarnings: suppresses compiler warnings.

What is a comment?

Annotation is actually a special tag in the code, which is used to replace the configuration file, that is, the traditional way to tell the class how to run through the configuration file, with the annotation technology, the developer can tell the class how to run through annotations. A typical application of annotations in Java technology is that you can use reflection techniques to get annotations in a class to determine how to run the class.

Key points of annotation techniques:

How to define annotations

How to reflect annotations and decide how to run the class based on the reflected annotation information

2.1 Custom comments:

Define a new Annotation type using the @ interface keyword

Declare the properties of the annotation

The role of annotation attributes: the information originally written in the configuration file can be described by the attributes of the annotations.

The property declaration method of Annotation: String name ()

Attribute default value declaration method: Stringname () default "xxx"

Special attribute value: if there is an attribute with the name value in the annotation, you can omit the value= section when using the annotation, such as @ MyAnnotation ("xxx")

Special property value []

The types of annotation properties can be:

String Typ

Basic data type

Class Typ

Enumerated type

Annotation type

One-dimensional array of the above types

Case demonstration 1 create and use annotations

Public @ interface MyAnnocation {

String name ()

Int num () default 10

MyAnnocation2 anno ()

}

Public @ interface MyAnnocation2 {

String value ()

}

Public class Demo1 {

@ MyAnnocation (name= "", num=50,anno=@MyAnnocation2 (value = "xxx"))

Public void show () {

System.out.println ("xxxxxxx")

}

}

2.2 Meta Annotation of JDK

Meta Annotation refers to the Annotation that modifies Annotation.

Retention: can only be used to modify an Annotation definition to specify the domain that the Annotation can retain, and @ Rentention contains a member variable of type RetentionPolicy, through which the domain is specified.

RetentionPolicy.CLASS: the compiler will record the comments in the class file. When running the Java program, JVM does not retain comments. This is the default value.

RetentionPolicy.RUNTIME: the compiler will record the comments in the class file. When you run the Java program, JVM retains comments. The program can get the comment through reflection

RetentionPolicy.SOURCE: the compiler directly discards comments for this policy

@ Target: specifies which member of the class the annotation modifies. @ Target contains a member variable named value of type ElementType.

Documented: used to specify that the Annotation class modified by the meta Annotation will be extracted into a document by the javadoc tool.

Inherited: the Annotation modified by it will have inheritance. If a class uses Annotation decorated with @ Inherited, its subclasses will automatically have this annotation.

Case demonstration 2 uses reflection to get annotation information

@ Retention (RetentionPolicy.RUNTIME)

Public @ interface PersonInfo {

String name ()

Int age () default 20

String gender ()

}

Public class PersonOpe {

@ PersonInfo (name= "Li Si", age=20,gender= "male")

Public void show (String name,int age,String gen) {

System.out.println (name)

System.out.println (age)

System.out.println (gen)

}

}

Public class Demo2 {

Public static void main (String [] args) throws Exception {

PersonOpe ope=new PersonOpe ()

Class class1=PersonOpe.class

Method method = class1.getMethod ("show", String.class,int.class,String.class)

PersonInfo annotation = method.getAnnotation (PersonInfo.class)

String name=annotation.name ()

Int age=annotation.age ()

String gender=annotation.gender ()

Method.invoke (ope, name,age,gender)

}

}

This is the end of the content of "how to use Java Notes". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report