springboot配置之apollo配置源码解析 - Go语言中文社区

springboot配置之apollo配置源码解析


入口是在invokeBeanFactoryPostProcessors方法中。
apollo中有一个类,PropertySourcesProcessor,类结构图如下:

这个类继承了BeanFactoryPostProcessor,会执行其postProcessBeanFactory方法
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    initializePropertySources();
  }
然后在initializePropertySources方法中读取了配置,并放入到了Spring的类StandardServletEnvironment中

  protected void initializePropertySources() {
    if (environment.getPropertySources().contains(APOLLO_PROPERTY_SOURCE_NAME)) {
      //already initialized
      return;
    }
    CompositePropertySource composite = new CompositePropertySource(APOLLO_PROPERTY_SOURCE_NAME);

    //sort by order asc
    ImmutableSortedSet<Integer> orders = ImmutableSortedSet.copyOf(NAMESPACE_NAMES.keySet());
    Iterator<Integer> iterator = orders.iterator();

    while (iterator.hasNext()) {
      int order = iterator.next();
      for (String namespace : NAMESPACE_NAMES.get(order)) {
        Config config = ConfigService.getConfig(namespace);

        composite.addPropertySource(new ConfigPropertySource(namespace, config));
      }
    }
    environment.getPropertySources().addFirst(composite);
  }
propertySources在AbstraceEnvironment类型中
private final MutablePropertySources propertySources = new MutablePropertySources(this.logger);

private final ConfigurablePropertyResolver propertyResolver =
      new PropertySourcesPropertyResolver(this.propertySources);
后面就可以通过Environtment.getProperty()获取apollo中的属性,Environment中还加入了别的propertySources,也都可以用Environment获取

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/lz710117239/article/details/81028981
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-12-12 00:48:00
  • 阅读 ( 1386 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢