【踩坑】解決springboot注入yml配置文件 list報錯

springboot中yml配置注入一般使用@Value註解可注入String類型數據,比如:

@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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章