springboot注入yml配置文件 list報錯的解決方案
更新時間:2021年08月18日 14:59:22 作者:風(fēng)舞葉揚
這篇文章主要介紹了springboot注入yml配置文件 list報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
springboot注入yml配置文件 list報錯
springboot中yml配置注入一般使用@Value注解可注入String類型數(shù)據(jù),比如:
@Value("${config}")
String stringConfig;
即可注入屬性,而注入list使用此方法則會報錯提示Could not resolve placeholder xxx。
注入list的正確方法
配置文件實例
list-config:
config:
- companyId
- userId
- originId
注入姿勢
@ConfigurationProperties(prefix = "list-config")
@Component
@Setter
public class VisitorSourceController implements VisitorSourceApi {
List<String> config;
}
注意:必須在類上添加Lombok的@Setter注解或者加上屬性set方法,否則config屬性會獲取到null。
springboot yml 配置文件注入Map,List
person:
lastName: hello
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 12
/**
* 將配置文件中配置的每一個屬性的值,映射到這個組件中
* @ConfigurationProperties:告訴SpringBoot將本類中的所有屬性和配置文件中相關(guān)的配置進行綁定;
* prefix = "person":配置文件中哪個下面的所有屬性進行一一映射
*
* 只有這個組件是容器中的組件,才能容器提供的@ConfigurationProperties功能;
*
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
springboot項目集成swagger-bootstrap-ui全過程
這篇文章主要介紹了springboot項目集成swagger-bootstrap-ui全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
Mybatis內(nèi)置參數(shù)之_parameter和_databaseId的使用
這篇文章主要介紹了Mybatis內(nèi)置參數(shù)之_parameter和_databaseId的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

