SpringBoot使用spring.config.import多種方式導(dǎo)入配置文件

簡介

SpringBoot從2.4.x版本開始支持了導(dǎo)入文件的方式來加載配置參數(shù),與spring.config.additional-location不同的是不用提前設(shè)置而且支持導(dǎo)入的文件類型相對(duì)來說要豐富很多。

我們只需要在application.properties/application.yml配置文件中通過spring.config.import屬性配置需要導(dǎo)入的文件列表即可。

通過spring.config.import屬性支持導(dǎo)入多種途徑的配置文件,下面簡單介紹幾種。

導(dǎo)入classpath下的配置文件

可以導(dǎo)入classpath下任意目錄的文件,使用方式如下所示:

spring:
  config:
    import:
    # 導(dǎo)入classpath下default目錄下的default.properties配置文件
    - classpath:/default/default.properties
    # 導(dǎo)入classpath下service目錄下的service.yml配置文件
    - classpath:/service/service.yml

src/main/resource下分別創(chuàng)建default、service目錄,在default目錄下創(chuàng)建default.properties、在service目錄下創(chuàng)建sevice.yml。

通過上面配置的屬性導(dǎo)入后我們直接就可以在項(xiàng)目中通過@ConfigurationProperties@Value來注入使用。

src/main/resource、src/main/java目錄編譯后都會(huì)到classpath根目錄下。

// default.properties
default.password=111111
// service.yml
service:
  id: example
  port: 9999
  index-path: /index
// default.properties
@Value("${default.password}")
private String defaultPassword;
---
// service.yml
@Configuration
@ConfigurationProperties(prefix = "service")
@Data
public class ServiceProperties {
    private String id;
    private int port;
    private String indexPath;
}

導(dǎo)入系統(tǒng)目錄下的配置文件

可以導(dǎo)入操作系統(tǒng)目錄下的配置文件,我在/Users/yuqiyu/Downloads目錄下創(chuàng)建了名為system.properties的文件,導(dǎo)入方式如下所示:

spring:
  config:
    import:
    # 導(dǎo)入系統(tǒng)目錄/Users/yuqiyu/Downloads下的system.properties配置文件
    - optional:/Users/yuqiyu/Downloads/system.properties

使用@ConfigurationProperties方式注入映射如下所示:

// system.properties
system.os=osx
system.jdk-version=11

// SystemProperties.java
@Data
@Configuration
@ConfigurationProperties(prefix = "system")
public class SystemProperties {
    private String os;
    private String jdkVersion;
}

導(dǎo)入Nacos配置中心的配置文件

NacosSpringCloud Alibaba發(fā)布了2021.0.1.0版本后對(duì)spring.config.import做了支持,可以直接通過加載Nacos Server內(nèi)指定的配置文件。

首先我們使用Docker來創(chuàng)建一個(gè)Nacos Server容器,步驟如下所示:

# 拉取nacos-server鏡像
docker pull nacos/nacos-server
# 創(chuàng)建并啟動(dòng)nacos-server容器
docker run --name nacos-quick -e MODE=standalone -p 8848:8848 -p 9848:9848 -d nacos/nacos-server:latest

訪問http://localhost:8848/nacos,使用默認(rèn)賬號(hào)nacos登錄后在public命名空間下創(chuàng)建一個(gè)名為spring-config-import-example.yamlYAML格式的配置文件,內(nèi)容如下所示:

config:
    source: nacos

SpringBoot項(xiàng)目中如果需要集成nacos,可以直接添加spring-cloud-starter-alibaba-nacos-config依賴,如下所示:

<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  <version>2021.0.1.0</version>
</dependency>

導(dǎo)入方式如下所示:

spring:
  cloud:
    nacos:
      server-addr: localhost:8848
  config:
    import:
    # 導(dǎo)入nacos配置中心的配置文件
    - optional:nacos:spring-config-import-example.yaml

在項(xiàng)目中同樣可以使用@ConfigurationProperties、@Value來注入配置參數(shù),如下所示:

@Value("${config.source}")
private String configSource;

總結(jié)

spring.config.import使用方式是多樣化的,如果你需要自定義導(dǎo)入的方式,可以借鑒nacos對(duì)其實(shí)現(xiàn)的部分代碼。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容