我在自己写点东西玩的时候需要读配置文件,又不想引包,于是打算扣点Spring Boot读取配置文件的代码出来,当然只是读配置文件没必要这么麻烦,不过反正闲着也是闲着,扣着玩了。
具体启动过程以前的博客写过Spring Boot启动过程(一),这次入口在SpringApplication类中:
    private ConfigurableEnvironment prepareEnvironment(
SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, applicationArguments.getSourceArgs());     //此处读取
listeners.environmentPrepared(environment);
if (isWebEnvironment(environment)
&& this.webApplicationType == WebApplicationType.NONE) {
environment = convertToStandardEnvironment(environment);
}
return environment;
}

  关于监听器的过程在开头说的那篇的一系列中也说的挺细的,这里不介绍了:

  都是监听器相关的部分,略了,SpringApplicationRunListeners类中:

    public void environmentPrepared(ConfigurableEnvironment environment) {
for (SpringApplicationRunListener listener : this.listeners) {
listener.environmentPrepared(environment);
}
}

  EventPublishingRunListener:

  onApplicationEnvironmentPreparedEvent事件触发org\springframework\boot\spring-boot\2.0.0.BUILD-SNAPSHOT\spring-boot-2.0.0.BUILD-20170421.122111-547-sources.jar!\org\springframework\boot\context\config\ConfigFileApplicationListener.java监听器执行:

现在这个postProcessors中包含Json之类其他的监听器,不过我现在只想扣出properties的代码,别的先略过,反正其实也没什么,本来也是想看看它的思路,扣着玩,不要太在意。

    protected void addPropertySources(ConfigurableEnvironment environment,
ResourceLoader resourceLoader) {
RandomValuePropertySource.addToEnvironment(environment);
new Loader(environment, resourceLoader).load();
}

        Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
this.environment = environment;
this.resourceLoader = resourceLoader == null ? new DefaultResourceLoader()
: resourceLoader;
}
        this.classLoader = ClassUtils.getDefaultClassLoader();
//其实也就是Thread.currentThread().getContextClassLoader();

  下面就是真正加载了配置文件的load方法了,先是初始化PropertySourcesLoader和一些临时的集合:

            this.propertiesLoader = new PropertySourcesLoader();
this.activatedProfiles = false;
this.profiles = Collections.asLifoQueue(new LinkedList<Profile>());
this.processedProfiles = new LinkedList<>(); // Pre-existing active profiles set via Environment.setActiveProfiles()
// are additional profiles and config files are allowed to add more if
// they want to, so don't call addActiveProfiles() here.
Set<Profile> initialActiveProfiles = initializeActiveProfiles();
this.profiles.addAll(getUnprocessedActiveProfiles(initialActiveProfiles));

  这些集合其实如果没配置Profile基本是没用的,这东西现在已经很少用到了,这个环境当然是没配的:

  主要是下面这部分:

                for (String location : getSearchLocations()) {
if (!location.endsWith("/")) {
// location is a filename already, so don't search for more
// filenames
load(location, null, profile);
}
else {
for (String name : getSearchNames()) {
load(location, name, profile);
}
}
}

  就是去指定目录下去找各种以application为名字的指定类型的配置文件:

  我只关心application.properties,它是上面循环中的一次,走进了doLoadIntoGroup方法的下面那句:

    private Map<String, ?> loadProperties(Resource resource) throws IOException {
String filename = resource.getFilename();
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
return (Map) PropertiesLoaderUtils.loadProperties(resource);
}
return new OriginTrackedPropertiesLoader(resource).load();
}

  这个resource其实只是封装了一下InputStream,具体的读取。。。反正也没啥特别的读法:

  读出的key和value放在Map<String, OriginTrackedValue>:

    private void put(Map<String, OriginTrackedValue> result, String key,
OriginTrackedValue value) {
if (!key.isEmpty()) {
result.put(key, value);
}
}

  以上。

==========================================================

咱最近用的github:https://github.com/saaavsaaa

微信公众号:

                      

Spring Boot的properties配置文件读取的更多相关文章

  1. spring boot 项目从配置文件中读取maven 的pom.xml 文件标签的内容。

    需求: 将pom.xml 文件中的版本号读取到配置文件并打印到日志中. 第一步: 在pom.xml 中添加以下标签. 第二步: 将version 标签的值读取到配置文件中 这里使用 @@  而不是  ...

  2. spring boot(5)-properties参数配置

     application.properties application.properties是spring boot默认的配置文件,spring boot默认会在以下两个路径搜索并加载这个文件 s ...

  3. 【转】spring boot application.properties 配置参数详情

    multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...

  4. Inspection info: Checks Spring Boot application .properties configuration files. Highlights unresolved and deprecated configuration keys and in

    Cannot resolve class or package ‘jdbc’ less… (Ctrl+F1) Inspection info: Checks Spring Boot applicati ...

  5. Spring boot application.properties 配置

    原文链接: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.ht ...

  6. @Value和@PropertySource实现*.properties配置文件读取过程和实现原理

    @Value和@PropertySource实现*.properties 配置文件读取过程和实现原理 1       配置使用步骤 (1)右击resource目录添加*.prooerties配置文件

  7. Spring Boot 的核心配置文件有哪几个?它们的区别是什么?

    Spring Boot 的核心配置文件是 application 和 bootstrap 配置文件.application 配置文件这个容易理解,主要用于 Spring Boot 项目的自动化配置.b ...

  8. Spring Boot中注入配置文件application.properties中的list 对象参数

    例如要注入下列参数: dyn.spring.datasources[0].name=branchtadyn.spring.datasources[0].driverClassName=oracle.j ...

  9. spring boot application.properties 属性详解

    2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...

随机推荐

  1. Robot framework的介绍

    Robot framework是基于Python语言编写的功能自动化测试框架.使用简单,不懂编码的测试人员也能像编程一样写测试用例,支持关键字驱动测试并且可以开发系统关键字.还有丰富的第三方库,比如S ...

  2. python os模块常用方法

    OS模块是Python标准库中的一个操作模块,主要用于处理Linux操作系统中的文件和目录 1.要使用OS必须先导入OS import os 2.os.getcwd()获取当前路径. print os ...

  3. JAVA----类的继承1(extends)

    要学习类的继承,首先应当理解继承的含义: 来自新华词典的释义: ①依法承受(死者的遗产等):-权ㄧ-人. ②泛指把前人的作风.文化.知识等接受过来:-优良传统ㄧ-文化遗产. ③后人继续做前人遗留下来的 ...

  4. file_get_contents HTTP request failed! Internal Server Error

    使用file_get_contents报错 Severity: WarningMessage: file_get_contents(http://geetest.com:8000/select?gid ...

  5. 【2017-04-20】Sql字符串注入式攻击与防御

    一.攻击 所谓sql字符串注入式攻击就是在用户输入界面输入一串sql语句,来改变C#中连接数据库要执行的sql语句 通过你写的程序,直接来执行我想要执行的sql语句 例如:在这么一个程序中,sname ...

  6. div模拟输入框input/textarea

    //html<!--填写信息--> <div class="info-wrap"> <form class="formToCheck&quo ...

  7. 树莓派的GPIO编程

    作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁转载. 树莓派除了提供常见的网口和USB接口 ,还提供了一组GPIO(General Purpose Input/ ...

  8. python基础教程第二版 第一章

    1.模块导入python以增强其功能的扩展:三种方式实现 (1). >>> Import math >>> math.floor(32.9) 32.0 #按照 模块 ...

  9. 解析View的getDrawingCache方法

    1. View 的getDrawingCache方法 有时候需要将某个view的内容以图片的方式保存下来,感觉就和截图差不多,可以使用View 的getDrawingCache方法,返回一个Bitma ...

  10. 又拍云SSL证书全新上线,提供一站式HTTPS安全解决方案

    互联网快速发展,云服务早已融入每一个人的日常生活,而互联网安全与互联网的发展息息相关,这其中涉及到信息的保密性.完整性.可用性.真实性和可控性.又拍云上线了与多家国际顶级 CA 机构合作的数款OV & ...