Spring scans custom annotations

Table of contents

1: Define custom annotations

Two: Use custom annotations

3.1 Define the test file path using annotations

3.2 Inherit BeanPostProcessor and add spring scanning configuration

Four: Test



Directory Structure:

 

1: Define custom annotations

package com.gyrx.lifecycle.task2.config;

import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Indexed;

import java.lang.annotation.*;


@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Jindalong {
    String value() default "自定义注解";

}

Two: Use custom annotations

3.1 Define the test file path using annotations

package com.gyrx.lifecycle.task2.bean;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan(value = {"com.gyrx.lifecycle.task2.bean"})
public class CommScanTest {

}

3.2 Inherit BeanPostProcessor and add spring scanning configuration

During the bean initialization process, the BeanPostProcessor interface, which is the post-processor in Spring, is called. This interface is the extended interface provided by the Spring IOC container to facilitate the completion of bean instantiation, configuration and other initialization methods in the Spring container. Add some own processing before and after logic.

    After the bean is created, the postProcessAfterInitialization method of the postprocessor will be called, so you can use this method to customize it to let spring scan custom annotations.
You can refer to the previous article on the life cycle of beans, which will be improved again later.

package com.gyrx.lifecycle.task2.bean;

import com.gyrx.lifecycle.task2.config.Jindalong;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Component
public class AnnotationValidRegistrar implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
        if (methods != null) {
            for (Method method : methods) {
                Jindalong jindalong = AnnotationUtils.findAnnotation(method, Jindalong.class);
                // process
                if (jindalong != null) {
                    System.out.println(method.getName());
                    System.out.println(jindalong.value());
                }

            }
        }
        return bean;
    }
}

Four: Test

Determine whether the bean object is in the bean factory

package com.gyrx.lifecycle.task2.bean;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class BeanTest {

    public static void main(String[] args) {
        //通过能否获取自定义注解bean对象,判断spring是否扫描到
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(CommScanTest.class);
        annotationConfigApplicationContext.refresh();
        AnnotationValid userService = (AnnotationValid) annotationConfigApplicationContext.getBean("annotationValid");
        System.out.println(userService);

    }
}


Screenshot of results 

Guess you like

Origin blog.csdn.net/qq_44691484/article/details/127232158