繁体   English   中英

Spring Boot 2.x列表绑定问题

[英]Spring Boot 2.x list binding issues

升级到Spring Boot 2x后,我在绑定某些列表时遇到问题。 该代码在Spring 1.x中有效,现在在启动时引发绑定错误。 这是我的应用程序。

aws:
  geo-mappings:
    - name: USA
      regions:
        - us-west-2
        - us-west-1
        - us-east-1
        - us-east-2
    - name: California
      regions:
        - us-west-2

这是我的组件类...

package com.example.demo.config.aws;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by goer on 4/18/17.
 */
@Component
@Scope("singleton")
@ConfigurationProperties(prefix="aws")
public class AWSConfigProvider {

    private List<GeoMappingEntry> geoMappings = new ArrayList<>();

    public List<GeoMappingEntry> getGeoMappings() {
        return this.geoMappings;
    }


}

这是嵌套对象...

package com.example.demo.config.aws;

import java.util.ArrayList;
import java.util.List;


public class GeoMappingEntry {
    private String name;
    private List<String> regions = new ArrayList<>();

    public GeoMappingEntry(String name, List<String> regions) {
        this.name = name;
        this.regions = regions;
    }
}

当我尝试跑步时,我得到...


申请开始失败


描述:

无法将“ aws.geo-mappings”下的属性绑定到java.util.List:

Reason: Failed to bind properties under 'aws.geo-mappings' to java.util.List<com.example.demo.config.aws.GeoMappingEntry>

行动:

更新您的应用程序的配置

还有其他人遇到过同样的问题吗? 解决方案? 建议?

以防万一其他人正在寻找类似的问题。 事实证明,某些在Spring早期版本中不起作用的环境变量现在可以实际创建绑定,而这正是错误的来源。

在这种情况下,从环境var轻松绑定到诸如AWS_GEOMAPPINGS_0_REGIONS_0 = us-west-2以前并不起作用,但现在可以了。 在Spring Boot 2.0之前,从环境var进行设置的唯一方法是通过SPRING_APPLICATION_JSON传递JSON,该方法可以正常工作,但是如果您尝试使用封装了它的其他JSON(例如Terraform)进行部署,则会变得复杂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM