<Java> Java Annotations Introduction

Source: Internet
Author: User
Tags java web

Objective

When I was learning Java Web Development, I first saw that the annotations were not in Java, but in spring, and for a long time, I thought that annotations were spring-specific techniques, and later, when used in MyBatis, there were many annotations, In addition to the @override often seen in Java, it is doubtful that annotations are java. But suspicion is suspected, but the development of more than a year, never want to know the annotations this thing, let alone develop their own annotations.

Introduction to Java Annotations

Recently, there are a lot of extra XML files in the project, I always thought it was very bad, think that spring can be in the applicationcontext.xml of things with annotations into the class, so I would like to try to develop their own annotations.

First, look at the annotation package in Java.

  

As you can see from the IDE's logo, there are four annotations hidden in it, and these four annotations are the Java meta-annotations, which are annotated annotations. They are in turn:

1.Document: No parameters, which means that the annotation is included in the Javadoc

2.Inherited: No parameters, indicating that it can be inherited

3.Retention: Parameter (optional in next rerentionpolicy), indicating what level the note can be saved

1.Source: The source level, that is, this annotation is to the compiler to see, after watching the throw, will not be compiled, that is, in the. class file is not found, such as @override

2.Class: Can be found in the Class file, but cannot be found while the VM is running. This is the default level.

3.Runtime: Can be preserved while the VM is running, so you can read the annotation information through the Java reflection mechanism, which is generally the most used. For example: @Value, @Controller, @Bean

4.Target: Parameter (optional in ElementType), indicating which level of the class the note can be placed on, by default is all.

With these, you can already develop your own annotations, and try:

@Retention (retentionpolicy.runtime) Public@InterfaceInfo {String book ()default"";} Public classBook {@Info ( Book= "Thinking in Java") String BookName;  PublicString GetName () {returnBookName; }         Public Static voidMain (string[] args) {field[] fields= Book.class. Getdeclaredfields ();  for(Field field:fields) {if(Field.isannotationpresent (Info).class) ) {Info Info= Field.getannotation (Info.class); Book Book=NewBook (); Book.bookname=Info.book ();            System.out.println (Book.getname ()); }        }    }}    

Simple to explode, of course, a little bit of the basic knowledge of Java reflection. In general, we use frames such as spring,mybatis to annotate, do not write these analytic reflection code, because the framework to help us do it.

<Java> Java Annotations Introduction

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.