Simple understanding

tags: Java  annotation  

 

Annotations are often used in the spring framework.

Java supports annotations since 1.5.

Annotation understanding:

What are annotations?

Starting from JDK5, Java has added support for metadata, that is, annotations. There is a certain difference between annotations and annotations. Annotations can be understood as special marks in the code.These tags can be read during compilation, class loading, and runtime, and corresponding processing is performed. Through annotations, developers can embed supplementary information in the source code without changing the original code and logic.

How to use annotations?

@XXXXX

When defining classes and methods, add @XXX

@MyFirstAnnotation(value = "test")
    public  void test() throws NoSuchMethodException {
                 //Get the annotations above the method
        MyFirstAnnotation annotation = this.getClass().getMethod("test").getAnnotation(MyFirstAnnotation.class);
 // Use of annotation objects
        System.out.println("method="+annotation.method());
        System.out.println("value="+annotation.value());
    }

How to write a comment yourself?

/**
* Customize a comment
* @Documented Define whether the annotation appears in the javadoc, optional
* @Target(ElementType.TYPE) Specify the type attribute of Annotation, specify that it is a class, interface, or method that it modifies, optional, if not specified, it can be used everywhere
* @Retention(RetentionPolicy.RUNTIME) Specify the policy attributes of the annotation, optional runtime or class loading
 * @interface defines annotations, which means that the Annotation interface is implemented
 *
 */

The underlying implementation is a dynamic proxy.

package com.wang.annotation;


import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

public class AnnotationTest {


    public static void main(String[] args) throws NoSuchMethodException {
        AnnotationTest annotationTest = new AnnotationTest();
        annotationTest.test();
    }

    /**
     * Use of annotations
           * The test method has an annotation MyFirstAnnotation
     * @throws NoSuchMethodException
     */

    @MyFirstAnnotation(value = "test")
    public  void test() throws NoSuchMethodException {
                 //Get the annotations above the method
        MyFirstAnnotation annotation = this.getClass().getMethod("test").getAnnotation(MyFirstAnnotation.class);
 // Use of annotation objects
        System.out.println("method="+annotation.method());
        System.out.println("value="+annotation.value());
    }
}

/**
   * Customize a comment
   * @Documented defines whether the annotation appears in javadoc, optional
   * @Target(ElementType.TYPE) Specify the type attribute of Annotation, specify that it is modified by the class, interface, or method, optional, not specified, it can be used everywhere
   * @Retention(RetentionPolicy.RUNTIME) Specify the policy attributes of the annotation, optional
   * @interface defines annotations, meaning that the Annotation interface is implemented
 *
 */

@Documented
//@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyFirstAnnotation {
    String value() default "";
    String method() default "POST";
}

 

Intelligent Recommendation

A simple understanding of C#-------------------- a simple understanding of C#

Let's first briefly describe what .NET, and a simple understanding of C#and one of them. 1.NET Overview .NET is the abbreviation of Microsoft.net, which is a technology based on the Windows platform. ...

Simple understanding and simple encapsulation of jQuery

1、Jquery Introduction to jQuery local download: Online introduction:http://www.bootcdn.cn/jquery/ Support all css2 selectors Most css3 options Cannot be quoted: this document window All click events a...

Python3 - simple understanding and simple operation

Reprinted at: https://blog.51cto.com/chaorenhuifei/1703026...

Simple understanding of simple bucket sorting

Assuming that the final exam is taken, the full score of the exam is 10, and five students have participated in the exam and scored 5, 3, 5, 2 and 8 points respectively. Now we need to sort their scor...

Simple understanding of AutoResetEvent

Work hard to become a savvy female programmer! During this time, I have contacted AutoResetEvent. After I have a simple understanding, I made a small demo to introduce the basic usage. Welcome everyon...

More Recommendation

Simple understanding of lazy and hungry

Single case design pattern Hungry Chinese : A class can only create one object Privatization constructor Create an instance of a class inside the class, and be static Privatization object, invoked thr...

Dynamic planning - simple understanding

Basic steps for dynamic planning Find out the nature of the optimal solution and characterize its structural features. (Looking for the sub-question structure of the optimal solution) Recursively defi...

A simple understanding of Jena and an example

A simple understanding of Jena and an example Jena is a city in northern Germany. Some people say that it is "a place that gives me a sense of belonging." This article briefly introduces Jen...

Simple understanding of the dichotomy

Simple understanding of the dichotomy The mathematical concept of the dichotomy is that for a function y=f(x) that is continuous over the interval {a,b} and satisfies f(a)f(b)<0, by constantly putt...

Axios simple understanding

Initiate a GET request Use directlyaxios('/user')method,axios()Method defaults to GET mode Using the axios.get() method, the parameters are written directly?key=valueForm, multiple use?key1=value1&...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top