天天看点

【踩坑】解决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。

继续阅读