rab*_*nda 7 java environment-variables spring-boot
我有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下环境变量运行应用程序时:
APP_PROP=val1
APP_NESTED_PROPS_PROP=val2
APP_NESTED_PROPS_NESTED_PROPS_PROP=val3
Run Code Online (Sandbox Code Playgroud)
我收到了服务的回复:
{
"prop": "val1",
"nestedProps": {
"prop": "val2",
"nestedProps": null
}
}
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?我期待这样的事情:
{
"prop": "val1",
"nestedProps": {
"prop": "val2",
"nestedProps": {
"prop": "val3",
"nestedProps": null
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我通过应用程序参数(例如:)设置属性时--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"));
}
Run Code Online (Sandbox Code Playgroud)
我会尝试使用 NESTED_NESTEDPROPS_PROP,bcs 'nestedProps' 是一个属性
属性名称中不得使用 _ 分隔符。即数据库平台必须写为 DATABASEPLATFORM 而不是 DATABASE_PLATFORM。
来源:https ://github.com/spring-projects/spring-boot/wiki/Relaxed-Binding-2.0
| 归档时间: |
|
| 查看次数: |
1294 次 |
| 最近记录: |