This is the 9th day of my participation in the August Wen Challenge.More challenges in August

AnnotationUtils, located below the Spring-core package, provides methods to easily get annotation information

1.findAnnotation

Gets the annotation annotation above the Class/Method annotation

// Get the annotation labeled above the Class
public static <A extends Annotation> A findAnnotation(Class<? > clazz,@Nullable Class<A> annotationType)

// Get the annotation information above Method
public static <A extends Annotation> A findAnnotation(Method method, @Nullable Class<A> annotationType)
Copy the code

case

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * Description:
 *
 * @author jack
 * @date2021/8/26 8:38pm */
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/api/main")
@Slf4j
public class MainController {

    @Bean(name = "car")
    public void car(a) {}public static void main(String[] args) {
        // Get the annotation above the Class
        RequestMapping requestMapping = AnnotationUtils.findAnnotation(MainController.class, RequestMapping.class);
        System.out.println(requestMapping);
        System.out.println(Arrays.toString(requestMapping.value()));
        
        // Get the annotation above Method
        Method carMethod = ReflectionUtils.findMethod(MainController.class, "car"); Bean bean = AnnotationUtils.findAnnotation(carMethod, Bean.class); System.out.println(Arrays.toString(bean.name())); }}Copy the code

The output

2.getAnnotationAttributes

Get all properties of the annotation

public static Map<String, Object> getAnnotationAttributes(Annotation annotation)
Copy the code

case

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Description:
 *
 * @author jack
 * @date2021/8/26 8:38pm */
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/api/main")
@Slf4j
public class MainController {

    public static void main(String[] args) {
        // Get the annotation above the Class
        RequestMapping requestMapping = AnnotationUtils.findAnnotation(MainController.class, RequestMapping.class);
        // Get all attributes of the RequestMapping annotationMap<String, Object> annotationAttributes = AnnotationUtils.getAnnotationAttributes(requestMapping); System.out.println(annotationAttributes); }}Copy the code

The output

3.isAnnotationDeclaredLocally

Verify that the annotation is directly annotated on the class rather than integrated.

import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.web.bind.annotation.Mapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Description: * * @author jack * @date 2021/8/26 8:38pm */ @requiredargsconstructor @restController @requestMapping (value = "/api/main") @Slf4j public class MainController { public static void main(String[] args) { boolean isExistsRequestMapping = AnnotationUtils.isAnnotationDeclaredLocally(RequestMapping.class, MainController.class); System.out.println(isExistsRequestMapping); boolean isExistsMapping = AnnotationUtils.isAnnotationDeclaredLocally(Mapping.class, MainController.class); System.out.println(isExistsMapping); }}Copy the code

The output

@requestMapping is annotated on the MainController and @requestMapping is annotated on the MainController so the output is false when this code is called

AnnotationUtils.isAnnotationDeclaredLocally(Mapping.class, MainController.class)
Copy the code