今天又个需求,就是根据注解来判断是否接口为对外开放,那么启动spring容器的时候把这些注解修饰的bean name放进缓存当中。
/**
* 扫描注解添加服务到缓存以供判断时候为对外开放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得到并进行操作。
本文介绍了一种利用Spring框架的BeanDefinitionRegistryPostProcessor接口来扫描特定注解,并将这些注解修饰的Bean名称存储到缓存中以供后续判断是否为对外开放服务的方法。

被折叠的 条评论
为什么被折叠?



