SpringBoot Maven 项目启用默认配置文件 源码解读

maven 打包配置

  • maven 配置 profile 环境
<profiles>
    <profile>
    	<!-- 配置id -->
        <id>test</id>
        <activation>
        	<!-- 是否启用为默认的配置 -->
            <activeByDefault>true</activeByDefault>
        </activation>
        <!-- 其他参数 -->
        <properties>
            <env>test</env>
        </properties>
    </profile>
</profiles>
  • maven 配置需要打包的 resource 目录
<build>
	<!-- maven 打包时需要加载的resource目录配置 -->
    <resources>
        <resource>
        	<!-- 配置resource目录 -->
            <directory>src/main/resources</directory>
            <!-- 需要排除的目录 -->
            <excludes>
                <exclude>dev/*</exclude>
                <exclude>prod/*</exclude>
                <exclude>test/*</exclude>
            </excludes>
        </resource>

        <resource>
            <!--根据不同的环境,把对应文件夹里的配置文件打包-->
            <directory>src/main/resources/${env}</directory>
            <!-- 配置导出的文件路径 -->
            <targetPath>/new</targetPath>
        </resource>
    </resources>
</build>

SpringBoot 读取配置文件机制

源码地址

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot</artifactId>
  <version>2.0.5.RELEASE</version>
</dependency>

业务类 org.springframework.boot.context.config.ConfigFileApplicationListener

启动

入口方法:onApplicationEvent(ApplicationEvent event)

项目启动后发布事件ApplicationEvent,触发配置文件监听器加载配置

加载配置文件调用流程

  1. 读取配置

    方法:load()

    开始读取配置

  2. 初始化环境 profile

    方法:initializeProfiles()

    根据 spring.profiles.active 和 spring.profiles.include 加载相关的配置

  3. 读取各个location的文件

    方法:load(Profile profile, DocumentFilterFactory filterFactory,
    DocumentConsumer consumer)

    读取各个location的文件

    • 读取所有路径

      方法:getSearchLocations()

      如果 environment 里包含 spring.config.location,则从对应的路径去读取配置文件

      如果没有配,则从classpath:/,classpath:/config/,file:./,file:./config/ 倒序去获取

    • 读取所有配置文件名称

      方法:getSearchNames()

      如果 environment 里包含 spring.config.name ,则读取对应的配置文件

      如果没有,则依次加载:bootstrap、application

  4. 使用各个loader去读取

    方法:load(String location, String name, Profile profile,
    DocumentFilterFactory filterFactory, DocumentConsumer consumer)

    使用所有的loader遍历读取配置

    • 加载所有的loader

      是在初始化Loader的时候加载的

      • 实例化 Loader

        new Loader(environment, resourceLoader)

      • 加载各个 propertySourceLoader

        读取 LoaderpropertySourceLoaders

        方法:SpringFactoriesLoader.loadFactories(PropertySourceLoader.class, getClass().getClassLoader())

          默认读取到 YamlPropertySourceLoader 和 PropertiesPropertySourceLoader
        
          其中 YamlPropertySourceLoader 可以配置 "yml", "yaml"
        
          其中 PropertiesPropertySourceLoader 可以配置 "properties", "xml"
        
      • 读取所有的 propertySourceLoader

        方法: loadFactoryNames(factoryClass, classLoaderToUse)

        读取 factoryClass 的所有实现类

  5. 追加环境后缀

    方法:loadForFileExtension(PropertySourceLoader loader, String prefix,String fileExtension, Profile profile,DocumentFilterFactory filterFactory, DocumentConsumer consumer)

    如果有启用的环境,则加载配置文件时候加载对应环境后缀的配置

  6. 读取配置

    方法:load(PropertySourceLoader loader, String location, Profile profile, DocumentFilter filter, DocumentConsumer consumer)

    挨个加载配置文件内的属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值