Java Essentials Java annotations easy to get started

Source: Internet
Author: User

Annotations are simply configuration, which is a special configuration, and can be replaced with annotations. It then uses reflection to get the information of the annotations.

How to define an annotation

You create a new annotation definition in the IDE, which is structured like this:

package com.nicchagil.exercise.springbootexercise.annotation;public @interface MyFirstAnnotation {}

Then there are about 4 of the above structure plus some configuration, of course, this configuration is added in the form of annotations =_=!

Where is this annotation used

Where this annotation will be applied can be configured as follows:

Keep at what time

When to keep:

Annotations are reflected in the document

@Documented

Whether the subclass inherits annotations from the parent class

@Inherited

Get the information of annotations with reflection

Let's define an annotation first:

package com.nicchagil.exercise.springbootexercise.annotation;import java.lang.annotation.Documented;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)@Documentedpublic @interface PojoPostProcessing {        public Class targetClass();        public String[] whiteProperties();}

Use annotations, such as here I set to a method:

    @PojoPostProcessing(targetClass=User.class, whiteProperties={"name"})    public List<User> selectXxx(String id) {        ......    }

Reflection gets the information for the annotation:

    @Test    public void test() throws NoSuchMethodException, SecurityException, ClassNotFoundException {        Class clazz = Class.forName("com.nicchagil.exercise.springbootexercise.service.UserService");                Method method = clazz.getMethod("selectXxx", String.class);                boolean isAnnotationPresent = method.isAnnotationPresent(PojoPostProcessing.class);        if (!isAnnotationPresent) {            return;        }                PojoPostProcessing dpp = (PojoPostProcessing)method.getAnnotation(PojoPostProcessing.class);        this.logger.info("dpp : {}", dpp);    }

Log:

dpp : @com.nicchagil.exercise.springbootexercise.annotation.PojoPostProcessing(targetClass=class com.nicchagil.exercise.springbootexercise.mapper.entity.User, whiteProperties=[name])

Java Essentials Java annotations easy to get started

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.