如何使用 Spring 引导配置属性对属性进行分组
How to group properties with Spring Boot Configuration Properties
根据 Spring 引导文档,可以对属性进行分组,属性 可能出现在多个组中。但是当我们创建一个标有@ConfigurationProperties(prefix="test1") 的属性 class 时,组名将是test1 的前缀。现在,如果我有另一个 属性 class,例如前缀为 "test2",我怎么能说后一个有来自组 test1 的 属性?
---更新---
添加了嵌套 class 但它不起作用
@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;
}
}
内部 class 上的注释注释 @ConfigurationProperties 是在我的测试失败后完成的。 Spring 不会加载带或不带注释的那些属性,除非它们在同一个 属性 文件中,在本例中为 application-emx.properties。这是为什么?我想分开这些属性
=== 已解决 ====
我注意到我必须使用 getter/setter 方法添加嵌套 class 类型的字段,否则 Spring 不会加载嵌套 class
中的属性
你可以在内部 类:
的帮助下组合它们
属性 文件
test1.property1=...
test1.test2.property2=...
test1.test2.property3=...
Java/Spring映射:
import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Getter
@Setter
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(locations = "classpath:myapp.properties")
public class ApplicationProperties {
private String property1;
private Test2 test2;
@Getter
@Setter
@ConfigurationProperties(prefix = "test2")
public static class Test2 {
@NotNull
private String property2;
@NotNull
private String property3;
}
}
我们用这种方法取得了成功,因为 java 合成模仿了 属性 文件的结构。属性也是可验证的,因此如果配置不正确,您很快就会失败。
这种方法的缺点是属性是可变的。
如果您的属性文件变得太大,您的应用程序很可能存在更广泛的问题。
注释处理器自动将内部 classes 视为嵌套属性。确保已定义 getter 和 setter。
@ConfigurationProperties(prefix="server")
public class ServerProperties {
private String name;
private Host host;
// ... getter and setters !!!
public static class Host {
private String ip;
private int port;
// ... getter and setters !!!
}
}
使用 non-inner class 可以达到相同的效果,但是您应该在字段上使用 @NestedConfigurationProperty 注释来指示常规 (non-inner) class应该被视为嵌套。
@ConfigurationProperties(prefix="server")
public class ServerProperties {
private String name;
@NestedConfigurationProperty
private Host host;
// ... getter and setters !!!
}
public class Host {
private String ip;
private int port;
// ... getter and setters
}
根据 Spring 引导文档,可以对属性进行分组,属性 可能出现在多个组中。但是当我们创建一个标有@ConfigurationProperties(prefix="test1") 的属性 class 时,组名将是test1 的前缀。现在,如果我有另一个 属性 class,例如前缀为 "test2",我怎么能说后一个有来自组 test1 的 属性?
---更新--- 添加了嵌套 class 但它不起作用
@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;
}
}
内部 class 上的注释注释 @ConfigurationProperties 是在我的测试失败后完成的。 Spring 不会加载带或不带注释的那些属性,除非它们在同一个 属性 文件中,在本例中为 application-emx.properties。这是为什么?我想分开这些属性
=== 已解决 ==== 我注意到我必须使用 getter/setter 方法添加嵌套 class 类型的字段,否则 Spring 不会加载嵌套 class
中的属性你可以在内部 类:
的帮助下组合它们属性 文件
test1.property1=...
test1.test2.property2=...
test1.test2.property3=...
Java/Spring映射:
import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Getter
@Setter
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(locations = "classpath:myapp.properties")
public class ApplicationProperties {
private String property1;
private Test2 test2;
@Getter
@Setter
@ConfigurationProperties(prefix = "test2")
public static class Test2 {
@NotNull
private String property2;
@NotNull
private String property3;
}
}
我们用这种方法取得了成功,因为 java 合成模仿了 属性 文件的结构。属性也是可验证的,因此如果配置不正确,您很快就会失败。
这种方法的缺点是属性是可变的。
如果您的属性文件变得太大,您的应用程序很可能存在更广泛的问题。
注释处理器自动将内部 classes 视为嵌套属性。确保已定义 getter 和 setter。
@ConfigurationProperties(prefix="server")
public class ServerProperties {
private String name;
private Host host;
// ... getter and setters !!!
public static class Host {
private String ip;
private int port;
// ... getter and setters !!!
}
}
使用 non-inner class 可以达到相同的效果,但是您应该在字段上使用 @NestedConfigurationProperty 注释来指示常规 (non-inner) class应该被视为嵌套。
@ConfigurationProperties(prefix="server")
public class ServerProperties {
private String name;
@NestedConfigurationProperty
private Host host;
// ... getter and setters !!!
}
public class Host {
private String ip;
private int port;
// ... getter and setters
}