java - 在对应的Configuration类中配置多个yml文件(Spring Boot)
问题描述
我在资源类路径位置的 Spring Boot 中有多个 yml 文件,例如 Spring Boot 的以下结构。最初我只为 application-abc.yml 编写,当时这个文件的所有值都加载到它们相应的类中,但是当我添加到另一个文件 application-xyz.yml 时,它也会加载到它们相应的配置类中,但是此时只在两个配置类中加载 application-xyz.yml 的值。因此,需要帮助在单个构建中配置相应配置文件中的两个文件的值:
-src
-main
-java
-packages
-config
-ApplicationAbcConfig.java
-ApplicationConfig.java
-ApplicationFactory.java
-ApplicationXyzConfig.java
-Authentication.java
-Operations.java
-Payload.java
-RequestPayload.java
-ResponsePayload.java
-services
-YmlConfigurationSelection.java
-resources
-application.yml
-application-abc.yml
-application-xyz.yml
-MultipleYmlDemoProject.java
application-abc.yml 的内容
authentication:
name: name
type: type
payload:
request:
- sequence: 1
attributes:
- attributes1
- attributes2
response:
- sequence: 1
attributes:
- attributes3
- attributes4
operations:
name: name
type: type
payload:
request:
- sequence: 1
attributes:
- attributes5
- attributes6
response:
- sequence: 1
attributes:
- attributes7
- attributes8
application-xyz.yml 的内容
authentication:
name: name
type: type
payload:
request:
- sequence: 1
attributes:
- attributes9
- attributes10
response:
- sequence: 1
attributes:
- attributes11
- attributes12
operations:
name: name
type: type
payload:
request:
- sequence: 1
attributes:
- attributes13
- attributes14
response:
- sequence: 1
attributes:
- attributes15
- attributes16
ApplicationConfig.java 的内容
public interface ApplicationConfig {
public Authentication getAuthentication();
public void setAuthentication(Authentication authentication);
public Operations getOperations();
public void setOperations(Operations operations);
}
Authentication.java 的内容
public class Authentication {
private String name;
private String type;
private Payload payload;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Payload getPayload() {
return payload;
}
public void setPayload(Payload payload) {
this.payload = payload;
}
}
Operations.java 的内容
public class Operations {
private String name;
private String type;
private Payload payload;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Payload getPayload() {
return payload;
}
public void setPayload(Payload payload) {
this.payload = payload;
}
}
Payload.java 的内容
public class Payload {
private List<RequestPayload> request;
private List<ResponsePayload> response;
public List<RequestPayload> getRequest() {
return request;
}
public void setRequest(List<RequestPayload> request) {
this.request = request;
}
public List<ResponsePayload> getResponse() {
return response;
}
public void setResponse(List<ResponsePayload> response) {
this.response = response;
}
}
RequestPayload.java 的内容
public class RequestPayload {
private String sequece;
private List<String> attributes;
public String getSequece() {
return sequece;
}
public void setSequece(String sequece) {
this.sequece = sequece;
}
public List<String> getAttributes() {
return attributes;
}
public void setAttributes(List<String> attributes) {
this.attributes = attributes;
}
}
ResponsePayload.java 的内容
public class ResponsePayload {
private String sequece;
private List<String> attributes;
public String getSequece() {
return sequece;
}
public void setSequece(String sequece) {
this.sequece = sequece;
}
public List<String> getAttributes() {
return attributes;
}
public void setAttributes(List<String> attributes) {
this.attributes = attributes;
}
}
ApplicationAbcConfig.java 的内容
@Configuration
@SpringBootConfiguration
@EnableConfigurationProperties
@org.springframework.context.annotation.PropertySource("classpath:application-abc.yml")
public class ApplicationAbcConfig implements ApplicationConfig, PropertySourceFactory {
private Authentication authentication;
private Operations operations;
@Override
public Authentication getAuthentication() {
return authentication;
}
@Override
public void setAuthentication(Authentication authentication) {
this.authentication = authentication;
}
@Override
public Operations getOperations() {
return operations;
}
@Override
public void setOperations(Operations operations) {
this.operations = operations;
}
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
Properties propertiesFromYaml = loadYamlIntoProperties(resource);
String sourceName = name != null ? name : resource.getResource().getFilename();
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
}
private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
try {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
} catch (IllegalStateException e) {
// for ignoreResourceNotFound
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException)
throw (FileNotFoundException) e.getCause();
throw e;
}
}
}
ApplicationXyzConfig.java 的内容
@Configuration
@SpringBootConfiguration
@EnableConfigurationProperties
@org.springframework.context.annotation.PropertySource("classpath:application-xyz.yml")
public class ApplicationXyzConfig implements ApplicationConfig, PropertySourceFactory {
private Authentication authentication;
private Operations operations;
@Override
public Authentication getAuthentication() {
return authentication;
}
@Override
public void setAuthentication(Authentication authentication) {
this.authentication = authentication;
}
@Override
public Operations getOperations() {
return operations;
}
@Override
public void setOperations(Operations operations) {
this.operations = operations;
}
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
Properties propertiesFromYaml = loadYamlIntoProperties(resource);
String sourceName = name != null ? name : resource.getResource().getFilename();
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
}
private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
try {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
} catch (IllegalStateException e) {
// for ignoreResourceNotFound
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException)
throw (FileNotFoundException) e.getCause();
throw e;
}
}
}
ApplicationFactory.java 的内容
@Component
public class ApplicationFactory {
@Autowired
private ApplicationAbcConfig applicationAbcConfig;
@Autowired
private ApplicationXyzConfig applicationXyzConfig;
public ApplicationConfig getApplicationPropertiesConfig(String application) {
if (application.equalsIgnoreCase("abc")) {
return applicationAbcConfig;
} else if (application.equalsIgnoreCase("xyz")) {
return applicationXyzConfig;
} else {
return null;
}
}
}
YmlConfigurationSelection.java 的内容
public class YmlConfigurationSelection {
@Autowired
private ApplicationFactory applicationFactory;
private ApplicationConfig applicationConfig;
public Object accessingProperties(String application) {
applicationConfig = applicationFactory.getApplicationPropertiesConfig(application);
return null;
}
}
MultipleYmlDemoProject.java 的内容
@SpringBootApplication
@SpringBootConfiguration
@PropertySource(factory = ApplicationAbcConfig.class, value = "classpath:application-abc.yml")
@PropertySource(factory = ApplicationXyzConfig.class, value = "classpath:application-xyz.yml")
public class MultipleYmlDemoProject {
public class MultipleYmlDemo {
public static void main(String[] args) {
ConfigurableApplicationContext ctx =
SpringApplication.run(YamlPropertysourceApplication.class, args);
ConfigurableEnvironment env = ctx.getEnvironment();
}
}
}
解决方案
看起来您有一个试图迁移到 Spring Boot 的旧 Spring 应用程序。
Spring Boot 原生使用 yaml 文件,因此如果您以 Spring Boot 方式进行集成,则可以删除许多您必须维护的样板代码。配置的命名也是有问题的:这些名称application-<something>.yml
保留用于 Spring Boot 配置文件,也许如果你重命名myprops-abc/xyz.yaml
它会以不同的方式表现,我不能肯定地说。
总而言之,我建议您采用以下方式,这在 IMO 中要好得多:
- 两个配置集都必须加载到一个配置中,因此创建一个配置属性文件来表示这一配置:
@ConfigurationProperties(prexix="security")
public class SecurityConfigProperties {
private SecurityRealm abc;
private SecurityRealm xyz;
// getters, setters
}
public class SecurityRealm {
private Authentication autentication;
private Operations operations;
// getters setters
}
public class Authentication {...}
private class Operations {...}
- 现在将 abc 和 xyz yaml 中的所有内容放入一个文件中
application.yaml
,并给出一个“安全”前缀:
security:
abc: // note, this 'abc' matches the field name of the configuration
authentication:
...
operations:
....
xyz:
authentication:
...
operations:
....
- 好的,一切都已映射,创建如下配置:
@Configuration
@EnableConfigurationProperties(SecurityConfigProperties.class)
public class SecurityConfiguration {
@Bean
public SecurityBeanForABC(SecurityConfigProperties config) {
return new SecurityBeanForABC(config.getAbc().getAuthentication(), config.getAbc().getOperations());
}
... the same for XYZ
}
请注意,使用这种方法,您只需在 java 对象中维护配置映射,并且没有用于加载/解析属性的代码) - 一切都由 spring boot 自动完成。如果您配置了一个特殊的注释处理器并拥有一个下降 IDE,您甚至可以获得这些 yaml 属性的自动完成功能,但它超出了这个问题的范围。关键是,以 Spring Boot 直接支持的方式做事有很多优点 :)
推荐阅读
- sql - 从 spark 函数调用转换为 SQL
- sql - 比较不同表的两列的数据类型
- javascript - 使用 API 数据源根据日期在 Chart.js 上显示数据
- javascript - 服务器端无法捕获附加的表单数据文件
- kotlin - Kotlin 折叠函数是否需要类型转换或指定 lambda 类型?
- html - 如何使用 html 和 css 使表格中的图像适合单元格,并且不会溢出或扩展单元格
- java - 保存后如何将 MongoDB 集合加载到 Java 程序数据结构中?
- sql - 带有基于文本的字段的 SQL 透视
- python - 电子邮件注册确认 Django
- python - Swift - 如何为我的类定义一个返回其对象的字符串表示的特殊方法