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.
This commit is contained in:
Alex Tighe 2023-04-11 14:15:40 -04:00 committed by GitHub
parent 6904c81c12
commit 0ac00ad389
6 changed files with 95 additions and 0 deletions

View File

@ -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());
}
}

View File

@ -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<String> teachers;
List<String> students;
public void setTeachers(List<String> teachers) {
this.teachers = teachers;
}
public void setStudents(List<String> students) {
this.students = students;
}
public List<String> getTeachers() {
return teachers;
}
public List<String> getStudents() {
return students;
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,3 @@
students:
- Jane
- Michael

View File

@ -0,0 +1,3 @@
teachers:
- Margo
- Javier

View File

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