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 ---