【发布时间】:2016-03-07 17:05:52
【问题描述】:
根据 Spring Boot 文档,可以对属性进行分组,并且一个属性可能出现在多个组中。但是当我们创建一个用@ConfigurationProperties(prefix="test1") 标记的属性类时,组名将是前缀test1。现在,如果我有另一个属性类,例如前缀为“test2”,我怎么能说后者具有来自组 test1 的属性?
--- 更新 --- 添加了嵌套类,但它不起作用
@Configuration
@Profile({"wmx"})
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "myapp.wmx", locations = {"classpath:application-wmx.properties", "classpath:myapp-env.properties"})
public class WmxProperties {
/**
* The WMX implementation to be loaded.
*/
@NotNull(message = "Must be configured.")
private ProfileEnum profile;
//@ConfigurationProperties(locations = "classpath:myapp-env.properties")
public static class Env {
/**
* Host name for WMX.
*/
private String host;
/**
* Port number for WMX.
*/
//@Pattern(regexp = "^[1-9]\\d*$", message = "Positive port number only.")
private Integer port;
/**
* Provider name.
*/
@NotBlank
private String providerName;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getProviderName() {
return providerName;
}
public void setProviderName(String providerName) {
this.providerName = providerName;
}
}
public ProfileEnum getProfile() {
return profile;
}
public void setProfile(ProfileEnum profile) {
this.profile = profile;
}
}
内部类上的注释注释@ConfigurationProperties 是在我的测试失败后完成的。 Spring 不会加载带有或不带有注释的那些属性,除非它们在同一个属性文件中,在本例中为 application-emx.properties。这是为什么?我想把这些属性分开
=== 已解决 ==== 我注意到我必须使用 getter/setter 方法添加嵌套类类型的字段,否则 Spring 不会加载嵌套类中的属性
【问题讨论】:
-
我不确定你有什么属性。您能否举例说明您的属性以及到目前为止您对 ConfigurationProperties 的尝试?
标签: java spring properties configuration spring-boot