Interpretation of "Interview plus sub-item" Java-defined annotations

Source: Internet
Author: User

My previous blog has described the declaration of my own definition annotations today we look at how to use the annotations we define ourselves.

1. We use our annotations in the program.

In the previous article, we defined an annotation ourselves:

@Target(ElementType.FIELD)//注解的作用范围。就是注解是用在什么地方的@Retention(RetentionPolicy.RUNTIME)//注解的级别,就是注解能留存到什么时候@Documented@Inheritedpublic @interface MyAnnotation {    publicvalue();//注解能够接收的參数}

This annotation has only one parameter, so we do not need to display the description parameter name when we use it. Below we write a class and use annotations.

publicclass People {    @MyAnnotation("liu")    private String name;    publicPeople() {        Inject.injectfeild(this);    }    publicgetName(){        return name;    }}

We were able to see that we had written a class and wrote a note on top of him, if what we are going to do now is to assign the value in the annotation to the following property name, what should we do? Here we can see that we have called inject.injectfeild (this) in the constructor method, which is how we parse the annotations. Let's take a look at how to do it in detail.

2. Parsing the annotations

No more nonsense. First, the code:

 Public  class Inject {     Public Static void Injectfeild(Object o) {class<?> clazz = O.getclass ();//Get the class passed infield[] fields = Clazz.getdeclaredfields ();//Get the domain of the class by reflection (global variable)          for(Field field:fields) {//See if this field has our own definition of the annotation class flag             if(Field.isannotationpresent (Myannotation.class)) {Myannotation inject = field.getannotation (Myannotation.class);//Get to our annotationsString Value=inject.value ();//Get the number of annotationsField.setaccessible (true);Try{Field.set (o, value);//Assign values to our fields}Catch(IllegalArgumentException |                Illegalaccessexception e) {e.printstacktrace (); }             }         }    }}

In fact, we get the values of the annotations and the corresponding fields by reflection. He was assigned a value. Let's take a look at the test again:

publicclass Test {    publicstaticvoidmain(String[] args) {        People people=new People();        System.out.println(people.getName());    }}

Results:
Liu

Of course, usually a field assignment for a normal class we can give him a set function, and this example simply understands how to define annotations. In many frameworks, however, their properties are assigned in this way because they are interested in learning about the IOC in order to understand the coupling.


Because the source code that is uploaded to Csdn has not been reviewed yet. Need source code to be able to leave the email in the comment, I will send the past as soon as possible.

Interpretation of "Interview plus sub-item" Java-defined 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.