龙空技术网

spring boot 配置读取配置文件路径

铁骨柔晴 286

前言:

当前大家对“springboot获取当前项目的路径”大概比较关怀,咱们都需要学习一些“springboot获取当前项目的路径”的相关资讯。那么小编同时在网络上搜集了一些关于“springboot获取当前项目的路径””的相关知识,希望各位老铁们能喜欢,你们快快来学习一下吧!

一、spring.config.location

2.x版本 spring.config.location 设置后,只读取这个文件配置内容,不再读取其他地址的配置文件

根据官方文档表述,默认会读取

  1. file:./config/  2. file:./config/*/  3. file:./  4. classpath:/config/  5. classpath:/

在使用 spring.config.location 指定外部配置文件时,需要此份配置文件需全量满足当前工程运行时所需,因为它不会去与 resources 目录下的配置文件去做 merge 操作。

二、spring.config.additional-location

配置后加载顺序,属性会merge. 同名属性以优先级高的为准

  1. file:./custom-config/  2. classpath:custom-config/  3. file:./config/  4. file:./config/*/  5. file:./  6. classpath:/config/  7. classpath:/

三、spring.profiles.active

springboot 默认是在src/main/resources文件夹中加载application.properties默认配置文件,格式为application-{profile}.properties,其中{profile}对应你的环境标识

在application.properties中添加spring.profiles.active = dev,database

profile如果不设置,默认是default。

下面是官方文档的说明:

In addition to application.properties files, profile-specific properties can also be defined by using the following naming convention: application-{profile}.properties . The Environment has a set of default profiles (by default, [default] ) that are used if no active profiles are set. In other words, if no profiles are explicitly activated, then properties from application-default.properties are

loaded.

标签: #springboot获取当前项目的路径