Java Spring@ConfigurationProperties根配置
当通过Java Spring@ConfigurationProperties根配置,java,spring,spring-boot,Java,Spring,Spring Boot,当通过@ConfigrationProperties编写spring配置时,有没有一种方法可以连接配置的根值 我的配置类似于 foo.bar=hello foo.bar.baz=world 如何从foo.bar变量中获取值。现在我有这个 @Configuration @ConfigurationProperties("foo.bar") public class FooBar { private String baz; // Getters and setters... }
@ConfigrationProperties
编写spring配置时,有没有一种方法可以连接配置的根值
我的配置类似于
foo.bar=hello
foo.bar.baz=world
如何从foo.bar
变量中获取值。现在我有这个
@Configuration
@ConfigurationProperties("foo.bar")
public class FooBar {
private String baz;
// Getters and setters...
}
我无法更改模式。属性来自env variables
FOO_BAR
和FOO_BAR_BAZ
用@PropertySource
注释注释类,并添加一个变量来获取它
@PropertySource("classpath:config.properties")
public class FooBar {
@Value("${foo.bar}")
private String baz;
}
您需要使用
@enableConfigurationProperties(FooBar.class)
来告诉spring boot为您的配置创建一个bean。像这样的东西应该适合你:
@Configuration
@EnableConfigurationProperties(FooBar.class)
class MyConfiguration {
final FooBar fooBar;
MyConfiguration(FooBar fooBar) {
this.fooBar = fooBar;
}
@ConfigurationProperties("foo.bar")
static class FooBar {
String baz;
// ....
}
@Bean
MyBean myBean() {
// use this.fooBar here...
}
}
我写这篇文章是为了一个可能处于同样情况的人:
代码:
@配置
@配置属性(“foo.bar”)
@数据
@AllArgsConstructor
@NoArgsConstructor
公共级FooBar{ 私有字符串baz }
@NoArgsConstructor,安装并使用 只需使用
@Value
绑定属性,这可能是本例中最简单的方法。