spring 扫描自定义注解

目录

一:定义自定义注解

二:使用自定义注解

3.1 定义使用注解的测试文件路径

3.2继承BeanPostProcessor增加spring扫描配置

四:测试



目录结构:

一:定义自定义注解

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 "自定义注解";

}

二:使用自定义注解

3.1 定义使用注解的测试文件路径

package com.gyrx.lifecycle.task2.bean;

import org.springframework.context.annotation.ComponentScan;

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

}

3.2继承BeanPostProcessor增加spring扫描配置

在Bean初始化的过程都会调用BeanPostProcessor接口即Spring中的后置处理器,这个接口是Spring IOC容器给我们提供扩展接口,方便在Spring容器中完成bean实例化,配置以及其他初始化方法前后添加一些自己处理的逻辑。

    在bean创建好之后都会调用后置处理器的postProcessAfterInitialization方法,所以可以利用自定义这个方法,达到让spring扫描自定义注解的目的。
可以参考上一篇bean的生命周期,稍后会再次完善

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;
    }
}

四:测试

判断bean对象是否在bean工厂里面了

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);

    }
}


结果截图 

猜你喜欢

转载自blog.csdn.net/qq_44691484/article/details/127232158