天天看點

Spring Boot系列之讀取配置

使用SpringBoot架構開發,讀取配置是少不了的,那麼你會讀取配置嗎?你會寫配置嗎?List?Map?

1 目的

本節我們要解決如下幾個問題:

  • 如何使用Spring Boot讀取配置檔案?有哪些方式?
  • 常用的幾種資料結構,如字元串、整數、List、Map,如何配置?如何讀取?
  • 如何自定義配置檔案的路徑?

2 讀配置檔案

Spring Boot預設的配置檔案有兩種格式:

application.properties

application.yml

查找順序是首先從

application.properties

查找,如果找不到,再查找

application.yml

優先級:

application.properties > application.yml

首先,添加依賴。

Maven:

<!-- spring boot config -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>           

Gradle:

annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'           

2.1 使用 @Value 讀取配置

配置如下:

erwin.name=馮文議
erwin.age=20
erwin.sex=男
erwin.english-name=Erwin Feng
erwin.birthday=1992/02/26
erwin.like=movie,game,music,tea,travel
erwin.visitedCities=巴中,揭陽,廣州,從化,成都,三亞,上海,杭州,北京
erwin.moreOther={myWeb:'https://fengwenyi.com',github:'https://github.com/fengwenyi'}           

代碼如下:

package com.fengwenyi.spring_boot_config_sample.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.*;

/**
 * @author Erwin Feng
 * @since 2020/8/11
 */
@Data
@Configuration
public class ReadConfigByValue {

    @Value("${erwin.name}")
    private String name;

    @Value("${erwin.age}")
    private Integer age;

    @Value("${erwin.sex}")
    private String sex;

    @Value("${erwin.english-name}")
    private String englishName;

    @Value("${erwin.birthday}")
    private Date birthday;

    @Value("${erwin.like}")
    private List<String> likes;

    @Value("#{'${erwin.visitedCities}'.split(',')}")
    private List<String> visitedCities;

    @Value("#{${erwin.moreOther}}")
    private Map<String, String> moreOther;
    
}           

2.2 使用 @ConfigurationProperties 讀取配置

配置如下(properties格式)

author.name=馮文議
author.age=20
author.sex=男
author.english-name=Erwin Feng
author.birthday=1992/02/26
author.like[0]=movie
author.like[1]=game
author.like[2]=music
author.like[3]=tea
author.like[4]=travel
author.visitedCities=巴中,揭陽,廣州,從化,成都,三亞,上海,杭州,北京
author.moreOther.myWeb=https://fengwenyi.com
author.moreOther.github=https://github.com/fengwenyi           

配置如下(yaml格式)

author:
  name: 馮文議-yml
  age: 20
  sex: 男
  english-name: Erwin Feng
  birthday: 1992/02/26
  like:
    - movie
    - game
    - music
    - tea
    - travel
  visitedCities: 巴中,揭陽,廣州,從化,成都,三亞,上海,杭州,北京
  moreOther:
    myWeb: https://fengwenyi.com
    github: https://github.com/fengwenyi           
package com.fengwenyi.spring_boot_config_sample.config;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Erwin Feng
 * @since 2020/8/12
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "author")
public class AuthorConfig implements Serializable {

    private static final long serialVersionUID = 9032405467573421607L;

    private String name;

    private Integer age;

    private String sex;

    private String englishName;

    @JsonFormat(pattern = "yyyy/MM/dd")
    private Date birthday;

    private List<String> like;

    private List<String> visitedCities;

    private Map<String, String> moreOther;

}           

讀取出來的資料展示:

{
    "name":"馮文議",
    "age":20,
    "englishName":"Erwin Feng",
    "birthday":"1992/02/26",
    "likes":[
        "movie",
        "game",
        "music",
        "tea",
        "travel"
    ],
    "visitedCities":[
        "巴中",
        "揭陽",
        "廣州",
        "從化",
        "成都",
        "三亞",
        "上海",
        "杭州",
        "北京"
    ],
    "moreOther":{
        "myWeb":"https://fengwenyi.com",
        "github":"https://github.com/fengwenyi"
    }
}           

2.3 通過注入環境變量來擷取配置資訊

@Autowired
    private Environment environment;           

3 @Value 用法

3.1 設定預設值

格式:@Value("${name:defaultValue}")

當配置檔案找不到這個配置時,就會傳回預設值,如果沒有預設值,就會報錯。

3.2 可以直接讀取系統的屬性值

如:@Value("${java.home}")

D:JavaJava8jdk1.8.0_251jre

3.3 可以用在方法和參數上,當做單元測試

// 單元測試-讀取配置檔案的值
    @Value("${erwin.like}")
    public void testReadLike(String like, @Value("${erwin.sex}") String sex) {
        System.out.println("1===>" + like);
        System.out.println("1===>" + sex);
    }           

參數 like 會取 @Value("${erwin.like}") 的值

參數 sex 會取 @Value("${erwin.sex}") 的值

經過測試,多個方法,執行順序是随機。

特别注意:這個隻是在啟動的時候執行,但是實際調用的時候,還是傳入的值。

3.4 讀取list的值

方法一:

配置檔案:

erwin.like=movie,game,music,tea,travel

Java代碼:

@Value("${erwin.like}")

private List likes;

方法二:

erwin.visitedCities=巴中,揭陽,廣州,從化,成都,三亞,上海,杭州,北京

@Value("#{'${erwin.visitedCities}'.split(',')}")

private List visitedCities;

3.5 讀取Map的值

erwin.moreOther={myWeb:' https://fengwenyi.com ',github:' https://github.com/fengwenyi '}

@Value("#{${erwin.moreOther}}")

private Map moreOther;

3.6 讀取系統屬性

@Value("#{systemProperties}")

private Map systemPropertiesMap;

3.7 給私有屬性指派

@Component
@PropertySource("classpath:values.properties")
public class PriorityProvider {
 
    private String priority;
 
    @Autowired
    public PriorityProvider(@Value("${priority:normal}") String priority) {
        this.priority = priority;
    }
 
    // standard getter
}           

4 @ConfigurationProperties

package com.fengwenyi.spring_boot_config_sample.config;

import com.fengwenyi.spring_boot_config_sample.support.YamlPropertySourceFactory;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.Serializable;

/**
 * @author Erwin Feng
 * @since 2020/8/13
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "db")
//@PropertySource({"classpath:config/db.properties"})
@PropertySource(value = {"classpath:config/db.yml"}, factory = YamlPropertySourceFactory.class)
public class DBConfig implements Serializable {

    private static final long serialVersionUID = -6527591545525817929L;

    /** 伺服器位址 */
    private String host;

    /** 端口 */
    private Integer port;

    /** 資料庫名 */
    private String dbName;

    /** 使用者名 */
    private String username;

    /** 密碼 */
    private String password;
}
           
db:
  host: localhost
  port: 3306
  db-name: test
  username: root
  password: 123456           

說明:

1、@Configuration 表明這是一個Spring配置,會注入到Spring容器中。

2、@ConfigurationProperties(prefix = "db") 表示這個類與配置檔案關聯,其中prefix指明字首。

3、@PropertySource 這個指明配置檔案的位置,如果沒有這個配置,則會從預設的配置檔案讀取。預設的配置檔案:application.properties > application.yml。另外,這個預設隻支援properties類型的檔案,是以,需要配置factory。

4、@PropertySource也可以與@Value結合使用。

YamlPropertySourceFactory

package com.fengwenyi.spring_boot_config_sample.support;

import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;

import java.io.IOException;

/**
 * @author Erwin Feng
 * @since 2020/8/13
 */
public class YamlPropertySourceFactory implements PropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        // 傳回 yaml 屬性資源
        return new YamlPropertySourceLoader()
                .load(resource.getResource().getFilename(), resource.getResource())
                .get(0);
    }
}           

參考連結

關于

我是馮文議(Erwin Feng),Java Developer,專注于程式設計與開發。開源項目:JavaLib、api-result。喜歡電影、遊戲、音樂、茶、旅行。

我的個人網站:

我的Github: