spring 配置文件加载原理

1. 实现 PropertySourceLocator 接口,比如:NacosPropertySourceLocator

2. 把实现注册到spring 容器

@Bean
    public NacosPropertySourceLocator nacosPropertySourceLocator(NacosConfigManager nacosConfigManager) {
        return new NacosPropertySourceLocator(nacosConfigManager);
    }

3.  PropertySourceBootstrapConfiguration会调用所有的PropertySourceLocator,做properties加载。

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(PropertySourceBootstrapProperties.class)
public class PropertySourceBootstrapConfiguration implements
        ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {

注:ApplicationContextInitializer的用途,在spring 容器刷新之前,会调用ApplicationContextInitializer.intialize()方法。

posted @ 2022-01-13 14:14  软件匠工  阅读(276)  评论(0编辑  收藏  举报