首頁  >  文章  >  Java  >  springboot怎麼讀取yml檔案中的list列表、陣列、map集合和對象

springboot怎麼讀取yml檔案中的list列表、陣列、map集合和對象

PHPz
PHPz轉載
2023-05-11 10:46:102545瀏覽

application.yml定義list集合

第一種方式使用@ConfigurationProperties註解取得list集合的所有值

type:
  code:
    status:
      - 200
      - 300
      - 400
      - 500

編寫設定檔對應的實體類別,這裡要注意的是,定義list集合,先定義一個配置類別Bean,然後使用註解#@ConfigurationProperties註解來取得list集合值,這裡給大家講解下相關註解的作用

  • @Component 將實體類別交給Spring管理

  • @ConfigurationProperties(prefix = “type.code”) 讀取yml檔案中的list

  • @Data 自動產生getter和setter方法

#如下圖所示

package com.o2o.data;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Component
@ConfigurationProperties(prefix = "type.code") // 配置文件的前缀
@Data
public class TypeCodeConfig {
    private List<String> status;

    public void setStatus(List<String> status){
        this.status = status;
    }
    public List<String> getStatus(){
        return status;
    }
}

然後在要使用的地方自動注入,我是直接在啟動類別中讀取這個list,需要注意,使用yml中配置的list需要先將物件注入,然後透過get方法讀取設定檔中的的值。

  • @Autowired private TypeCodeConfig typeCodeConfig; 使用註解將物件注入

  • System.out.println(typeCodeConfig.getStatus()); 呼叫呼叫 getter方法讀取值

package com.o2o;

import com.o2o.data.TypeCodeConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.o2o.mapper")
public class AutoTestApplication implements CommandLineRunner {

	public static void main(String[] args) {
		SpringApplication.run(AutoTestApplication.class, args);
	}

	@Autowired
	private TypeCodeConfig typeCodeConfig;

	@Override
	public void run(String... args) throws Exception {
		System.out.println(typeCodeConfig.getStatus());

啟動springboot我們已經從控制台成功讀取到yml檔案中list集合的所有值了

springboot怎麼讀取yml檔案中的list列表、陣列、map集合和對象

第二種方式使用@value註解取得list集合的所有值

yml檔案配置如下

student:
  ids:
    - 7
    - 8
    - 9

然後建立一個實體類別

@Data
public class Student {
    @Value("${student.ids}")
    private List<Integer> ids;

}

再新建一個對list屬性的設定類別

@Component
@ConfigurationProperties(prefix = "student")
@Data
public class TypeCodeConfig {

private List<Integer> ids;

   public void setIds(List<Integer> ids) {
       this.ids = ids;
   }
      public  List<Integer> getIds(){
       return ids;
}

在啟動類別中註入

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.o2o.mapper")
public class AutoTestApplication implements CommandLineRunner {

	public static void main(String[] args) {
		SpringApplication.run(AutoTestApplication.class, args);
	}

	@Autowired
	private TypeCodeConfig typeCodeConfig;
	
	@Override
	public void run(String... args) throws Exception {

		System.out.println(typeCodeConfig.getIds());
	}

啟動springboot我們已經從控制台成功讀取到yml檔案中list集合的所有值了

springboot怎麼讀取yml檔案中的list列表、陣列、map集合和對象

application.yml定義數組類型

yml設定檔如下圖所示

dataSync: enable: true type: - "1" - "2" - "3"

透過@value註解取得數組值

@Value("${dataSync.enable.type}")
 private String[] type;

也可以透過建立組態類別bean,使用@ConfigurationProperties註解取得,如下圖所示:

@Data
@Component
@ConfigurationProperties(prefix = "dataSync.enable") // 配置 文件的前缀
public class InterceptorPathBean
{  
    private String[] type;
}

yml檔案也可以存放物件和物件的集合,使用方法與基本類型類似。
簡單範例:

定義map集合配置

interceptorconfig:
  path:
    maps:
      name: 小明
      age: 24

透過建立設定類別bean,使用@ConfigurationProperties註解取得map值,如下圖所示

@Data
@Component
@ConfigurationProperties(prefix = "interceptorconfig.path") // 配置 文件的前缀
public class InterceptorPathBean
{
    private Map<String , String> maps;
}

使用對象配置

student:
  id: 1
  name: Bruce
  gender: male

使用物件集合配置

students: 
  - id: 1
    name: Bruce
    gender: male
  - id: 2
    name: ...
    ...

這裡我要為大家總結一些需要重要的點:

1、list類型的yml設定檔中,需要使用"- "來組成一個列表集合。

2、yml中的前綴沒有層級限制,如果是多層級,例如這裡的demo/code,在java類別中配置ConfigurationProperties註解的prefix就寫作"demo.code"

# 3.屬性名稱在yml檔案中支援連字號"-",例如four-span,在java類別中配置屬性就需要轉為駝峰式,fourSpan。

4、java類別屬性需要配置set,get方法。

以上是springboot怎麼讀取yml檔案中的list列表、陣列、map集合和對象的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除