问题描述
 
     我有一个spring启动应用程序,并且想要在一个用@ConfigurationProperties注释的类中设置环境变量,这是一个二级嵌套属性,它是驼峰式的。 
     以下是该类的示例: 
@SpringBootApplication
@EnableConfigurationProperties(SpringApp.Props.class)
@RestController
public class SpringApp {
  private Props props;
  @ConfigurationProperties("app")
  public static class Props {
    private String prop;
    private Props nestedProps;
    public String getProp() {
      return prop;
    }
    public void setProp(String prop) {
      this.prop = prop;
    }
    public Props getNestedProps() {
      return nestedProps;
    }
    public void setNestedProps(Props nestedProps) {
      this.nestedProps = nestedProps;
    }
  }
  @Autowired
  public void setProps(Props props) {
    this.props = props;
  }
  public static void main(String[] args) {
    SpringApplication.run(SpringApp.class, args);
  }
  @RequestMapping("/")
  Props getProps() {
    return props;
  }
}
当我尝试使用以下环境变量运行应用程序时:
APP_PROP=val1
APP_NESTED_PROPS_PROP=val2
APP_NESTED_PROPS_NESTED_PROPS_PROP=val3
我收到了服务的回复:
{
  "prop": "val1",
  "nestedProps": {
    "prop": "val2",
    "nestedProps": null
  }
}
这是预期的行为吗? 我期待这样的事情:
{
  "prop": "val1",
  "nestedProps": {
    "prop": "val2",
    "nestedProps": {
      "prop": "val3",
      "nestedProps": null
    }
  }
}
 
     当我通过应用程序参数设置属性时(例如: - --app.prop=val1 --app.nestedProps.prop=val2 --app.nestedProps.nestedProps.prop=val3 ),我得到了预期的响应。 
有没有使用环境变量的解决方法,而没有修改代码来获得预期的行为?
 
     注意:我做了一些调试,看起来问题源于org.springframework.boot.bind.RelaxedNames没有为这种情况生成候选者。 
     这是我做的一个测试来证明它(它失败了): 
@Test
public void shouldGenerateRelaxedNameForCamelCaseNestedPropertyFromEnvironmentVariableName() {
  assertThat(new RelaxedNames("NESTED_NESTED_PROPS_PROP"), hasItem("nested.nestedProps.prop"));
}
1楼
我会尝试使用NESTED_NESTEDPROPS_PROP,bcs'nestedProps'是一个属性
不能在属性名称中使用_分隔符。 即数据库平台必须写为DATABASEPLATFORM而不是DATABASE_PLATFORM。
来源: :