在使用SpringBoot開發中需要將一些配置引數放在yml檔案中定義,再通過Java類來引入這些配置引數

SpringBoot提供了一些註解來實現這個功能

  • ConfigurationProperties
  • Value
  • EnableConfigurationProperties

下面提供例子來說明如何引入常規變數,陣列,List,Map,引用物件。

[相關程式碼-GitHub]

引入pom

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

<!--lombok 外掛,非必須 -->
<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
</dependency>

註解類

這裡定義了一個全域性的註解類,

@Data
@ToString
@ConfigurationProperties(prefix = "all")
public class AllConfigurationProperties {

//普通變數
private String name;
   //引用物件
private OtherProperties other = new OtherProperties();
//陣列
private String[] server;
//list
private List list;
//map
private Map map;
//複雜map
private Map<String, ModuleConfig> modules = new LinkedHashMap();
//複雜list
private List<ModuleConfig> modulesList; }
ConfigurationProperties:標明者是一個配置類,需要prefix配置yml中的配置字首。

需要注意幾點
1. 配置類中的名稱應當符合JavaBean的命名方式
2. 配置類中的名稱應當與yml中的相同,否則應使用@Value指定
比如:
yml檔案:

all:
  name: libai
------------------------------------
@Value("${all.name}")
private String myName;
3. 如果已經使用@Value方式,可不用寫Setter方法。否則必須為該變數寫Setter方法,這裡使用lombok的註解@Data來配置,會自動生成Setter,Getter,ToString方法

4. 預設值設定:
  (1)當使用@Value時,可以通過如下方式實現   @Value("${nzrpc.netty.port:8321}")
       private int nport;
     當yml沒有配置nzrpc.netty.port 時,預設值便是8321
  (2) 或者是直接對變數賦值
    private int nport = 8321 ;
    private  OtherProperties other = new OtherProperties();
 

上述配置類的引用物件
@Data
public class ModuleConfig {
private static final long serialVersionUID = 5508512956753757169L;
private String name;
private String version;
private String owner;
} @Data
public class OtherProperties { private Long id;
private String version;
}

使能配置類

@Slf4j
@EnableConfigurationProperties(AllConfigurationProperties.class)
@Configuration
public class AutoConfiguration { @Autowired
AllConfigurationProperties properties; @PostConstruct
public void init(){ System.out.println("properties = " + properties); } }

這裡使用@EnableConfigurationProperties使能配置類。它會為AllConfigurationProperties注入yml中的配置引數,並建立一個bean,後續可使用@Autowired注入使用

@Configuration註明這是一個SpringBoot的配置類

使用方法init()輸出配置.

yml中配置

all:
name: libai
other:
id: 100
version: 1.0.1 server:
- 127.0.0.1
- 127.0.0.2
- 127.0.0.3 list:
- 111
- 222
- 333 map:
key1: value1
key2: value2
key3: value3 modules:
key1:
name: modules-name-1
version: modules-version-1
owner: modules-owner-1
key2:
name: modules-name-2
version: modules-version-2
owner: modules-owner-2 modulesList:
- name: modules-name-3
version: modules-version-3
owner: modules-owner-3
- name: modules-name-4
version: modules-version-4
owner: modules-owner-4

輸出

properties =
AllConfigurationProperties(
name=libai,
other=OtherProperties(id=100, version=1.0.1),
server=[127.0.0.1, 127.0.0.2, 127.0.0.3],
list=[111, 222, 333],
map={
   key1=value1,
    key2=value2,
   key3=value3
   },
modules={
   key1=ModuleConfig(name=modules-name-1, version=modules-version-1, owner=modules-owner-1),
   key2=ModuleConfig(name=modules-name-2, version=modules-version-2, owner=modules-owner-2)
   },
modulesList=[
   ModuleConfig(name=modules-name-3, version=modules-version-3, owner=modules-owner-3),
   ModuleConfig(name=modules-name-4, version=modules-version-4, owner=modules-owner-4)
   ])