Getting Started with Java annotations

Source: Internet
Author: User

These days in learning spring3.x, found that many frameworks are now using the Java annotation function, and then their own Java annotations on this aspect of the preliminary study.

First of all, annotations and annotations are not meant to be, and they are not the same thing at all.

Note is the usual in our normal interpretation of the code, that is, the common "//..." and "/*..............*/", I believe we are also very familiar with, here is not much to say.

For annotations, can be said to be a very useful technology, what is the annotation, in fact, the annotation is as a description, is a description of the code, and can be read, can be written in the program when the file is a description of the code. (Personal understanding)

We usually write code in some Ides, such as Eclipse, may encounter some code with a wavy line or a line in the middle of the code, will tell you do not advocate the use of this method, said this method may be eliminated in later versions of the prompt,

This is actually the annotation that is helping you.

Because after java5.0 (jdk1.5), Java default built-in @deprecated annotation, his existence is the above function, in addition there are @override, @SuppressWarnings, as for the other two of the role, we have to explore.

The power of annotations is that we can customize the annotations we need!

First, annotations are the same type as the class and interface.

Defining the use of an annotation is almost the same as defining an interface, with the difference being one more @:

@interface myannotation{

String value () default "NULL";

}

This defines a note that belongs to you.

Annotations themselves are also code, and of course annotations can be described.

This is a bit weird to read, but this is an important feature, and annotations are annotations that describe the scope of the annotation and the scope of survival.

There are four descriptions of annotations, including:

@Target ({Elementtype.method})//Describes the scope of the annotation, Elementtype.method is a parameter, which means that it can only be used for methods, such as

@MyAnnotation ()//This is wrong

public class annotataiontest{

@MyAnnotataion ()//This is correct

public void Test () {

}

}

The remaining three species function as follows:

@Retention

Indicates at what level the annotation information needs to be saved. The optional retentionpolicy parameters include:

SOURCE: Annotations are discarded by the compiler

Class: Annotations are available in the class file, but are discarded by the VM

RUNTIME:VM will retain annotations during the run, so the information of the annotations can be read through the reflection mechanism.

@Document

To include annotations in the Javadoc

@Inherited

Allow subclasses to inherit annotations from parent class

What's the use of custom annotations? How to use it?

for the use of annotations, I used to write an example of their own, there is nothing bad where everyone taps--#

First, create a new custom annotation

Package My.annotation.i;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import Java.lang.annotation.Target; @Target ({elementtype.method}) @Retention (retentionpolicy.runtime) public @interface myannotation {String value () default "NULL";}

Then write a class that tests the annotations

Package My.annotation.impl;import My.annotation.i.myannotation;public class Annotationtest {@MyAnnotation Method 1 ") Public String method1 (string s) {System.out.println (" method1 are Invoked,params is "+s); return s;}   Public String method2 (string s) {   System.out.println ("Method2 are Invoked,params is" +s);   return s;   }   @MyAnnotation ("This was Method 3") Public   String method3 (string s) {   System.out.println ("Method3 is invoked, Params is "+s");   return s;   }}

Finally write a client, the annotations will be the

Package My.annotation.client;import Java.lang.reflect.annotatedelement;import Java.lang.reflect.method;import My.annotation.i.myannotation;import My.annotation.impl.annotationtest;public class Client {public static void main ( String[] args) throws Exception{class cs = Class.forName ("My.annotation.impl.AnnotationTest");        Method[] methods = Cs.getdeclaredmethods ();        for (Method method:methods) {        if (method.isannotationpresent (Myannotation.class)) {        Myannotation atest = Method.getannotation (myannotation.class);            Method.invoke (Cs.newinstance (), Atest.value ());}}}        

The output is as follows:

Method1 is Invoked,params are This is method 1
METHOD3 is Invoked,params are This is method 3

This is my preliminary understanding of the Java annotations, there may be something wrong with the place, everyone understanding--#

Getting Started with Java annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.