溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springboot怎么讀取yml文件中的list列表、數(shù)組、map集合和對象

發(fā)布時間:2023-02-24 11:48:04 來源:億速云 閱讀:149 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“springboot怎么讀取yml文件中的list列表、數(shù)組、map集合和對象”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“springboot怎么讀取yml文件中的list列表、數(shù)組、map集合和對象”吧!

application.yml定義list集合

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

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

編寫配置文件對應(yīng)的實(shí)體類,這里需要注意的是,定義list集合,先定義一個配置類Bean,然后使用注解@ConfigurationProperties注解來獲取list集合值,這里給大家講解下相關(guān)注解的作用

  • @Component 將實(shí)體類交給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()); 調(diào)用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我們已經(jīng)從控制臺成功讀取到y(tǒng)ml文件中l(wèi)ist集合的所有值了

springboot怎么讀取yml文件中的list列表、數(shù)組、map集合和對象

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

yml文件配置如下

student:
  ids:
    - 7
    - 8
    - 9

然后創(chuàng)建一個實(shí)體類

@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我們已經(jīng)從控制臺成功讀取到y(tǒng)ml文件中l(wèi)ist集合的所有值了

springboot怎么讀取yml文件中的list列表、數(shù)組、map集合和對象

application.yml定義數(shù)組類型

yml配置文件如下圖所示

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

通過@value注解獲取數(shù)組值

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

也可以通過創(chuàng)建配置類bean,使用@ConfigurationProperties注解獲取,如下圖所示:

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

yml文件還可以存放對象和對象的集合,使用方法與基本類型類似。
簡單舉例:

定義map集合配置

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

通過創(chuàng)建配置類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: ...
    ...

這里我給大家總結(jié)一些需要重要的點(diǎn):

1、list類型的yml配置文件中,需要使用"-"來組成一個列表集合。

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

3、屬性名稱在yml文件中支持連字符"-",比如four-span,在java類中配置屬性就需要轉(zhuǎn)為駝峰式,fourSpan。

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

到此,相信大家對“springboot怎么讀取yml文件中的list列表、數(shù)組、map集合和對象”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI