Spring 获取自定义注解所有类


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Set;


public class ComponentScannerConfigurerBk implements ApplicationListener, ApplicationContextAware {
    private ComponentScannerConfigurer registrationCenter;


    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        Class annotationClass = ComponentDesc.class;
        Map beanWhithAnnotation = applicationContext.getBeansWithAnnotation(annotationClass);
        Set> entitySet = beanWhithAnnotation.entrySet();
        for (Map.Entry entry :entitySet){
            Class clazz = entry.getValue().getClass();//获取bean对象
            System.out.println("================"+clazz.getName());
            ComponentDesc componentDesc = AnnotationUtils.findAnnotation(clazz,ComponentDesc.class);
            System.out.println("==================="+componentDesc.channel());

        }
    }
}

 

你可能感兴趣的:(Spring)