1. 概述
在設計 Spring Boot 應用程序時,我們通常希望使用外部配置來定義應用程序的屬性。 這樣可以使我們在不同的環境中共享相同的代碼。 在某些情況下,我們可能希望將屬性定義在多個 YAML 配置文件中,即使在相同的環境中也是如此。
在本教程中,我們將學習兩種方法來在創建 Spring Boot 應用程序時加載多個 YAML 配置文件。
2. 使用 Spring 配置文件
通過使用 Spring 配置文件,可以包含多個 YAML 配置文件的應用程序。
這種方法利用了 Spring 對應用程序配置文件(與配置文件關聯)的自動加載功能。
接下來,我們通過一個使用兩個 .yml 文件的示例進行説明。
2.1 YAML 設置
我們的第一個文件列出了學生信息。我們將它命名為 application-students.yml,並將其放置在我們的 ./src/main/resources 目錄下:
students:
- Jane
- Michael讓我們為第二個文件命名為application-teachers.yml,並將其放置在同一目錄./src/main/resources下:
teachers:
- Margo
- Javier2.2. 應用
現在,讓我們設置我們的示例應用程序。我們將使用應用程序中的<em >CommandLineRunner</em>來觀察我們的屬性加載情況:
@SpringBootApplication
public class MultipleYamlApplication implements CommandLineRunner {
@Autowired
private MultipleYamlConfiguration config;
public static void main(String[] args) {
SpringApplication springApp = new SpringApplication(MultipleYamlApplication.class);
springApp.setAdditionalProfiles("students", "teachers");
springApp.run(args);
}
public void run(String... args) throws Exception {
System.out.println("Students: " + config.getStudents());
System.out.println("Teachers: " + config.getTeachers());
}
}在此示例中,我們使用 setAdditionalProfiles() 方法程序化地設置了額外的 Spring 配置文件。
我們還可以使用 spring.profiles.include 參數在通用的 application.yml 文件中進行配置:
spring:
profiles:
include:
- teachers
- students兩種方法都可以設置配置文件,在應用程序啓動時,Spring 會加載任何遵循 application-{profile}.yml模式的 YAML 配置文件。
2.3. 配置
為了完成我們的示例,讓我們創建一個配置類。該類從 YAML 文件中加載屬性:
@Configuration
@ConfigurationProperties
public class MultipleYamlConfiguration {
List<String> teachers;
List<String> students;
// standard setters and getters
}讓我們檢查運行應用程序後的日誌:
c.b.p.m.MultipleYamlApplication : The following 2 profiles are active: "teachers", "students"
以下是輸出:
Students: [Jane, Michael]
Teachers: [Margo, Javier]儘管這種方法有效,但其主要缺點在於它使用了 Spring 配置文件功能,這很可能並非 Spring 框架的設計意圖。
鑑於此,我們來看一種更健壯的方法,用於包含多個 YAML 文件。
3. 使用 @PropertySources 註解
我們可以通過結合使用 @PropertySource 註解和加載 YAML 配置文件,來指定多個 YAML 配置文件的路徑。
3.1. 應用程序
讓我們再次嘗試一個類似的應用程序:
@SpringBootApplication
public class MultipleYamlApplication implements CommandLineRunner {
@Autowired
private MultipleYamlConfiguration config;
public static void main(String[] args) {
SpringApplication.run(MultipleYamlApplication.class);
}
public void run(String... args) throws Exception {
System.out.println("Students: " + config.getStudents());
System.out.println("Teachers: " + config.getTeachers());
}
}我們應該注意的是,在這個例子中,我們沒有設置 Spring 配置文件。
3.2. 配置和PropertySourceFactory
現在,讓我們來實現我們的配置類:
@Configuration
@ConfigurationProperties
@PropertySources({
@PropertySource(value = "classpath:application-teachers.yml", factory = MultipleYamlPropertySourceFactory.class),
@PropertySource(value = "classpath:application-students.yml", factory = MultipleYamlPropertySourceFactory.class)})
public class MultipleYamlConfiguration {
List<String> teachers;
List<String> students;
// standard setters and getters
}帶有@PropertySources註解的組件包含針對我們應用程序中每個 YAML 文件的@PropertySource。factory是一個自定義的PropertySourceFactory,它允許加載 YAML 文件:
public class MultipleYamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(encodedResource.getResource());
Properties properties = factory.getObject();
return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
}
}運行我們的MultipleYamlApplication,我們觀察到以下預期輸出:
Students: [Jane, Michael]
Teachers: [Margo, Javier]4. 結論
在本文中,我們探討了兩種在 Spring Boot 應用程序中加載多個 YAML 配置文件的方法。