码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot 获取yaml配置文件信息

时间:2019-11-14 18:33:11      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:img   creat   配置文件   运行   framework   throw   empty   文件   stand   

Spring boot 项目启动过程中:

org.springframework.boot.SpringApplication#prepareEnvironment

当程序步入listeners.environmentPrepared(environment);这里后,就会读取配置文件中信息。

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
            ApplicationArguments applicationArguments) {
        // Create and configure the environment
        ConfigurableEnvironment environment = getOrCreateEnvironment();
        configureEnvironment(environment, applicationArguments.getSourceArgs());
        listeners.environmentPrepared(environment);
        bindToSpringApplication(environment);
        if (!this.isCustomEnvironment) {
            environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
                    deduceEnvironmentClass());
        }
        ConfigurationPropertySources.attach(environment);
        return environment;
    }

技术图片

 

技术图片

 

这句代码不好调试listeners.environmentPrepared(environment); 

但可以换一种方式,找到上图中的这个类:org.springframework.boot.env.OriginTrackedMapPropertySource

技术图片

 

 在断点处打上断点,就可以看到系统是如何运行的:下面是堆栈信息技术图片

 

 

在堆栈信息中,可以看到这个类:org.springframework.boot.context.config.ConfigFileApplicationListener

这个类里面的包含一些配置信息:

private static final String DEFAULT_PROPERTIES = "defaultProperties";

    // Note the order is from least to most specific (last one wins)
    private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";

    private static final String DEFAULT_NAMES = "application";

    private static final Set<String> NO_SEARCH_NAMES = Collections.singleton(null);

    private static final Bindable<String[]> STRING_ARRAY = Bindable.of(String[].class);

    /**
     * The "active profiles" property name.
     */
    public static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active";

    /**
     * The "includes profiles" property name.
     */
    public static final String INCLUDE_PROFILES_PROPERTY = "spring.profiles.include";

    /**
     * The "config name" property name.
     */
    public static final String CONFIG_NAME_PROPERTY = "spring.config.name";

    /**
     * The "config location" property name.
     */
    public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";

一个properties. 一个yaml配置sourceloader

技术图片

 

 

加载yaml文件的类是YamlPropertySourceLoader

public class YamlPropertySourceLoader implements PropertySourceLoader {

    @Override
    public String[] getFileExtensions() {
        return new String[] { "yml", "yaml" };
    }

    @Override
    public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
        if (!ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", null)) {
            throw new IllegalStateException(
                    "Attempted to load " + name + " but snakeyaml was not found on the classpath");
        }
        List<Map<String, Object>> loaded = new OriginTrackedYamlLoader(resource).load();
        if (loaded.isEmpty()) {
            return Collections.emptyList();
        }
        List<PropertySource<?>> propertySources = new ArrayList<>(loaded.size());
        for (int i = 0; i < loaded.size(); i++) {
            String documentNumber = (loaded.size() != 1) ? " (document #" + i + ")" : "";
            propertySources.add(new OriginTrackedMapPropertySource(name + documentNumber, loaded.get(i)));
        }
        return propertySources;
    }

}

技术图片

 

 

注如果是云环境,则首先加载的是bootstrap.yml, environment也是StandardEnvironment,非servlet环境。这是和Spring boot有很大的不同,加载完成后,再嵌套一StandardServletEnvironment.

 技术图片

 

技术图片

 

 

 

技术图片

 加载完成后,再嵌套一StandardServletEnvironment.

技术图片

 

Spring Boot 获取yaml配置文件信息

标签:img   creat   配置文件   运行   framework   throw   empty   文件   stand   

原文地址:https://www.cnblogs.com/hankuikui/p/11854954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!