From 0ac00ad38900df36e9f61d616a76e1976e29b77f Mon Sep 17 00:00:00 2001 From: Alex Tighe <599747+atighe@users.noreply.github.com> Date: Tue, 11 Apr 2023 14:15:40 -0400 Subject: [PATCH] BAEL-6229: loading multiple yaml files in spring boot (#13724) * BAEL-6229: committing new example for loading multiple yaml configuration files * BAEL-6229: updating README.md * BAEL-6229: adding example one code, commented out * Revert "BAEL-6229: updating README.md" This reverts commit 51cd2dcf97f797aa6a723888fd246ef0142242a3. * BAEL-6229: adding comments around commented out code for first example. --- .../multipleyaml/MultipleYamlApplication.java | 27 ++++++++++++++ .../MultipleYamlConfiguration.java | 35 +++++++++++++++++++ .../MultipleYamlPropertySourceFactory.java | 23 ++++++++++++ .../main/resources/application-students.yml | 3 ++ .../main/resources/application-teachers.yml | 3 ++ .../src/main/resources/application.yml | 4 +++ 6 files changed, 95 insertions(+) create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlApplication.java create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlConfiguration.java create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlPropertySourceFactory.java create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/resources/application-students.yml create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/resources/application-teachers.yml diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlApplication.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlApplication.java new file mode 100644 index 0000000000..9dc754b24b --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlApplication.java @@ -0,0 +1,27 @@ +package com.baeldung.properties.multipleyaml; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MultipleYamlApplication implements CommandLineRunner { + + @Autowired + private MultipleYamlConfiguration config; + + public static void main(String[] args) { + SpringApplication springApp = new SpringApplication(MultipleYamlApplication.class); + + // Code from first example, uncomment to use multiple profiles + // 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()); + } +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlConfiguration.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlConfiguration.java new file mode 100644 index 0000000000..c46a321e24 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlConfiguration.java @@ -0,0 +1,35 @@ +package com.baeldung.properties.multipleyaml; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.PropertySources; + +import java.util.List; + +@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 teachers; + List students; + + public void setTeachers(List teachers) { + this.teachers = teachers; + } + + public void setStudents(List students) { + this.students = students; + } + + public List getTeachers() { + return teachers; + } + + public List getStudents() { + return students; + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlPropertySourceFactory.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlPropertySourceFactory.java new file mode 100644 index 0000000000..c09bd3c8b9 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/multipleyaml/MultipleYamlPropertySourceFactory.java @@ -0,0 +1,23 @@ +package com.baeldung.properties.multipleyaml; + +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.env.PropertiesPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.support.EncodedResource; +import org.springframework.core.io.support.PropertySourceFactory; + +import java.io.IOException; +import java.util.Properties; + +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); + } +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application-students.yml b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application-students.yml new file mode 100644 index 0000000000..f4aaf8ae4d --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application-students.yml @@ -0,0 +1,3 @@ +students: + - Jane + - Michael \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application-teachers.yml b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application-teachers.yml new file mode 100644 index 0000000000..f0d2080164 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application-teachers.yml @@ -0,0 +1,3 @@ +teachers: + - Margo + - Javier \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml index 2e28da5ad3..2c44065028 100644 --- a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml @@ -2,6 +2,10 @@ bael: root-level-property: defaultRootLevelValue spring: profiles: +# Multiple profiles for MultipleYamlApplication first example +# include: +# - teachers +# - students group: multidocument-integration: multidocument-integration-extension ---