如何在 Spring Boot 2.2.4 中将@ConstructorBinding 和@PropertySource 与@ConfigurationProperties 一起使用?
How to use @ConstructorBinding and @PropertySource together with @ConfigurationProperties with in Spring Boot 2.2.4?
我是 Spring Boot 新手。目前,我正在尝试创建一个 POJO class (SystemProperties.class) 来读取属性文件中的值 (parameter.properties 与 application.properties 分开,但仍在同一目录下 /src/main/resources。当我在 class 中使用 @ConstructorBinding 以使其不可变时,会发生此问题。
- @ConstructorBinding needs to be used with @EnableConfigurationProperties or @ConfigurationPropertiesScan.
- @ConfigurationPropertiesScan will ignore @Configuration annotation which is needed when using @PropertySource to specify external
*.properties file.
A) SystemProperties.class
@Configuration
@PropertySource("classpath:parameter.properties")
@ConstructorBinding
@ConfigurationProperties(prefix = "abc")
public class SystemProperties {
private final String test;
public SystemProperties (
String test) {
this.test = test;
}
public String getTest() {
return test;
}
B) parameter.properties
abc.test=text1
我已尝试删除 @PropertySource 注释,但无法检索该值,除非它来自 application.properties。非常感谢任何帮助!
解决这个问题的方法是将 class 分成两个 class 两个不同的关注点。使用这样的解决方案,您可以保留您创建的 SystemProperties class 并另外添加另一个 class 仅用于加载属性文件参数以使其可用于您的应用程序。
解决方案如下:
@ConstructorBinding
@ConfigurationProperties(prefix = "abc")
public class SystemProperties {
private final String test;
public SystemProperties(
String test) {
this.test = test;
}
public String getTest() {
return test;
}
}
请注意,我省略了 @Configuration
和 @PropertySource
注释。
@Configuration
@PropertySource("classpath:parameter.properties")
public class PropertySourceLoader {
}
请注意,我只是在一个新的 class 上添加了这个注释,专门为加载属性文件而创建。
最后,您可以在主应用程序 class 上添加 @ConfigurationPropertiesScan
以启用 属性 映射机制。
我是 Spring Boot 新手。目前,我正在尝试创建一个 POJO class (SystemProperties.class) 来读取属性文件中的值 (parameter.properties 与 application.properties 分开,但仍在同一目录下 /src/main/resources。当我在 class 中使用 @ConstructorBinding 以使其不可变时,会发生此问题。
- @ConstructorBinding needs to be used with @EnableConfigurationProperties or @ConfigurationPropertiesScan.
- @ConfigurationPropertiesScan will ignore @Configuration annotation which is needed when using @PropertySource to specify external
*.properties file.
A) SystemProperties.class
@Configuration
@PropertySource("classpath:parameter.properties")
@ConstructorBinding
@ConfigurationProperties(prefix = "abc")
public class SystemProperties {
private final String test;
public SystemProperties (
String test) {
this.test = test;
}
public String getTest() {
return test;
}
B) parameter.properties
abc.test=text1
我已尝试删除 @PropertySource 注释,但无法检索该值,除非它来自 application.properties。非常感谢任何帮助!
解决这个问题的方法是将 class 分成两个 class 两个不同的关注点。使用这样的解决方案,您可以保留您创建的 SystemProperties class 并另外添加另一个 class 仅用于加载属性文件参数以使其可用于您的应用程序。
解决方案如下:
@ConstructorBinding
@ConfigurationProperties(prefix = "abc")
public class SystemProperties {
private final String test;
public SystemProperties(
String test) {
this.test = test;
}
public String getTest() {
return test;
}
}
请注意,我省略了 @Configuration
和 @PropertySource
注释。
@Configuration
@PropertySource("classpath:parameter.properties")
public class PropertySourceLoader {
}
请注意,我只是在一个新的 class 上添加了这个注释,专门为加载属性文件而创建。
最后,您可以在主应用程序 class 上添加 @ConfigurationPropertiesScan
以启用 属性 映射机制。