zoukankan      html  css  js  c++  java
  • spring扫描自定义注解并进行操作

    转载: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);即可
    多谢大家

  • 相关阅读:
    分布式事务解决方案1--使用Atomikos分布式事务(事务强一致方案)
    SringBoot集成Sharding-Jdbc 实践
    Sharding-Jdbc简介
    Mycat+haproxy中使用keepalived保障haproxy的高可用
    Angular CLI
    背压(Backpressure)机制
    Function.identity()
    解决Error: ENOENT: no such file or directory, scandir 安装node-sass报错
    Reactor flatMap
    Reactor map
  • 原文地址:https://www.cnblogs.com/diyunpeng/p/6376157.html
Copyright © 2011-2022 走看看