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 treat java comments

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how to treat java notes, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

What are annotations?

Annotations can be described in one word, and that is metadata, which is a kind of data that describes data. Therefore, it can be said that annotations are the metadata of the source code.

@ Override

Public String toString () {

Return "This is String Representation of current object."

}

@ Override tells the compiler that this method is an override method (metadata that describes the method)

Why introduce annotations?

Before (and even after) the use of Annotation, XML is widely used to describe metadata.

But after extensive xml, developers find it more and more difficult to maintain XML.

They want to have something tightly coupled to the code to describe the code, rather than all using the description of XML loosely coupled to the code.

So the question is, when to use annotations and when to use xml?

Let's review the characteristics of the above two configurations:

For the way annotations are made. We will find that it is closely integrated with the code, so annotations are more suitable for doing operations that are highly code-related, such as exposing the services corresponding to Bean.

For XML configuration mode. We will find that it isolates the configuration from the code, so XML configuration is more suitable for doing global, code-independent operations, such as global configuration.

I believe that many people's previous understanding of annotations is to facilitate development. But in fact, the criterion for using annotations or XML should be: how relevant the configuration is to the code. If the code is highly relevant to the configuration, use annotated configuration, otherwise use XML configuration.

The role of annotations

Generate the document. This is the most common and the earliest comment provided by java. The commonly used ones are @ see @ param @ return and so on.

Track code dependencies to implement alternative configuration file functions. More common is the annotation-based configuration starting with spring 2.5. The function is to reduce configuration. Today's frameworks basically use this configuration to reduce the number of configuration files.

Format checking is performed at compile time. If @ Override is placed in front of the method, if your method does not override the superclass method, it can be checked at compile time.

The most mainstream application of annotations at present: instead of configuration files

Advantages and disadvantages of annotations

Note advantages: high development efficiency and low cost

Note disadvantages: high coupling and not conducive to later maintenance

How are annotations generated and used?

Step 1: customize comments

Step 2: use annotations

Step 3: analyze and comment

Annotations is just metadata and has nothing to do with business logic.

If the Annotations does not contain business logic, then someone must implement it. The "user" of metadata to do this. Annotations only provides information about the properties (classes / methods / packages / domains) it defines. The "consumer" of Annotations reads this information and implements the necessary logic. When we use the annotations provided by the framework, a component in the framework is responsible for the logical processing after reading.

The framework generally helps us to achieve the first step of custom annotations, and the third step of parsing annotations, our daily development process is basically the second step, using annotations.

Custom annotation

An annotation can be roughly divided into three parts: annotation body, meta-annotation, and annotation attributes.

The annotation body specifies the name of the annotation.

The meta-annotation marks the use scene, retention time and other information of the note.

The annotation attribute indicates the attribute that the annotation has.

Note disintegration

Note disassembly is the simplest component, which only needs to be modeled as in the example. The only difference from the declaration of the interface is the addition of an @ symbol before the interface keyword.

/ / declared an annotated body named sweet

@ Retention (RetentionPolicy.RUNTIME)

Public @ interface sweet {

}

Meta annotation

Meta-annotation is also an annotation itself, which is used to mark the retention time, usage scenarios, inheritance properties, and document generation information of general annotations.

Meta-annotation is a special annotation, which is included in the Java source code. There are only four meta-annotations in Java: @ Target, @ Retention, @ Documented, and @ Inherited.

@ Target comment

The Target annotation defines the scenarios in which it can be used.

It has the following values:

ElementType.ANNOTATION_TYPE can annotate a comment

ElementType.CONSTRUCTOR can annotate the constructor

ElementType.FIELD can annotate attributes

ElementType.LOCAL_VARIABLE can annotate local variables

ElementType.METHOD can annotate the method

ElementType.PACKAGE can annotate a package

ElementType.PARAMETER can annotate the parameters in a method.

ElementType.TYPE can annotate a type, such as classes, interfaces, enumerations.

@ Target ({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})

Public @ interface Autowired {

Boolean required () default true

}

In the above Autowire comments, the values of Target annotations are CONSTRUCTOR, METHOD, PARAMETER, FIELD and ANNOTATION_TYPE. This means that Autowired annotations can only be used in five scenarios: constructor, method, method parameter, attribute, and type.

@ Retention comment

Retention annotations are used to mark the retention time of this annotation.

It has four optional values:

RetentionPolicy.SOURCE . Comments are retained only during the source phase and will be discarded and ignored when the compiler compiles.

RetentionPolicy.CLASS . Annotations are only retained until compilation, and they are not loaded into JVM.

RetentionPolicy.RUNTIME . Annotations can be retained until the program runs, and it is loaded into the JVM, so they can be obtained while the program is running.

@ Retention (RetentionPolicy.RUNTIME) public @ interface Autowired {boolean required () default true;}

In the above Autowire comment, the value of the Retention comment is RetentionPolicy.RUNTIME, indicating that the comment will be retained until the program runs.

@ Documented

The @ Documented annotation means that the annotation information is written to the javadoc document.

By default, our annotation information is not written to the Javadoc document. However, if the annotation has the @ Documented flag, the annotation information is written to the javadoc document.

@ Inherited

The @ Inherited annotation identifies that the subclass will inherit the annotation properties of the parent class.

In the following example, we declare a Sweet annotation and then use the @ Sweet annotation in the Peach class, but not in the RedPeach class.

/ / declare a Sweet comment that identifies sweetness.

@ Inherited

@ Retention (RetentionPolicy.RUNTIME)

@ interface Sweet {}

/ / the peach has a sweet taste

@ Sweet

Public class Peach {}

/ / Red peaches

Public class RedPeach extends Peach {}

Although we didn't use the @ Sweet annotation on the RedPeach class, we used the @ Inherited annotation in the Sweet annotation declaration, so RedPeach inherits Peach's @ Sweet annotation.

Annotation attribute

The annotation attribute is similar to the declaration of the class method, and there are three pieces of information in the annotation property, namely: the attribute name, the data type, and the default value.

A boolean type data named required is declared in the @ Autowired annotation, and its default value is true.

Public @ interface Autowired {boolean required () default true;}

It is important to note that the data types of the attributes defined in the annotation must be 8 basic data types (byte, short, int, long, float, double, boolean, char) or classes, interfaces, annotations and their arrays.

Annotation analysis

What is analytical annotation? That is, the runtime annotation information on the class, function or member is obtained by reflection, so as to realize the logic of dynamically controlling the running of the program.

Parsing annotations mainly uses two class libraries:

1.1. Java.lang.annotation.Annotation

Java uses the Annotation interface to represent the comments in front of the program element, which is the parent interface of all Annotation types.

1.2. Java.lang.reflect.AnnotatedElement

The AnnotatedElement interface represents the annotated program elements in the program and is the parent interface of all program elements (Class, Method, Field, Package, and Constructor). After obtaining the interface object, you can call the object method to access the AnnotatedElement information. The following are commonly used:

1. GetAnnotations (): returns all comments that exist on the program element.

2. IsAnnotationPresent (annotation.class): determines whether the program element contains comments of the specified type

3. GetDeclaredAnnotations (): returns all comments that exist directly on this element. Unlike other methods in this interface, this method ignores inherited comments.

Case: automatic generation of sql through annotations (creation of tables)

Step 1: customize comments

Comments on the name of the table

Comments on table properties

Step 2: use annotations

Step 3: analyze and comment

On how to view the java notes to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report