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 获取自定义注解所有类
于 2019-06-11 08:45:51 首次发布