Spring 获取自定义注解所有类
Posted 醇氧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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<ContextRefreshedEvent>, 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<? extends Annotation> annotationClass = ComponentDesc.class;
Map<String,Object> beanWhithAnnotation = applicationContext.getBeansWithAnnotation(annotationClass);
Set<Map.Entry<String,Object>> entitySet = beanWhithAnnotation.entrySet();
for (Map.Entry<String,Object> entry :entitySet)
Class<? extends Object> clazz = entry.getValue().getClass();//获取bean对象
System.out.println("================"+clazz.getName());
ComponentDesc componentDesc = AnnotationUtils.findAnnotation(clazz,ComponentDesc.class);
System.out.println("==================="+componentDesc.channel());
以上是关于Spring 获取自定义注解所有类的主要内容,如果未能解决你的问题,请参考以下文章
spring 自定义注解(annotation)与 aop获取注解