轉載:http://blog.csdn.net/cuixuefeng1112/article/details/45331233
/**
* 掃描注解添加服務到緩存以供判斷時候為對外開放service
*/
@Component
@Lazy(true)
class AnnotationScannerConfigurer implements BeanDefinitionRegistryPostProcessor {
ArrayList<String> cache=RemoteCache.getCache();
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory postProcessBeanFactory) throws BeansException {
Map<String, Object> map=configurableListableBeanFactory.getBeansWithAnnotation(RemoteTag.class);
for (String key : map.keySet()) {
cache.add(key);
//System.out.println("beanName= "+ key );
}
}
}
我的AnnotationScannerConfigurer 實現了BeanDefinitionRegistryPostProcessor ,spring啟動時會執行postProcessBeanFactory方法,通過postProcessBeanFactory我們就可以拿到@RemoteTag所修飾的bean了。然后隨你怎么操作。
使用BeanDefinitionRegistryPostProcessor 是從mybaties的spring掃描實現類得到靈感的。
注意:@Lazy(true)是因為最后加載才能確保把所有@RemoteTag修飾的bean得到並進行操作。
SpringBoot:已經找到方法了。我用的是類繼承ApplicationListener<ContextRefreshedEvent>
然后用event.getApplicationContext().getBeansWithAnnotation(TableBind.class);即可
多謝大家