diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamlmap/MapFromYaml.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamlmap/MapFromYaml.java new file mode 100644 index 0000000000..93526c4a9f --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamlmap/MapFromYaml.java @@ -0,0 +1,13 @@ +package com.baeldung.properties.yamlmap; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MapFromYaml { + + public static void main(String[] args) { + SpringApplication.run(MapFromYaml.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamlmap/factory/YamlPropertySourceFactory.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamlmap/factory/YamlPropertySourceFactory.java new file mode 100644 index 0000000000..e46f7c9cba --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamlmap/factory/YamlPropertySourceFactory.java @@ -0,0 +1,25 @@ +package com.baeldung.properties.yamlmap.factory; + +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 YamlPropertySourceFactory 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-2/src/main/java/com/baeldung/properties/yamlmap/pojo/ServerProperties.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamlmap/pojo/ServerProperties.java new file mode 100644 index 0000000000..38b44fddc7 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yamlmap/pojo/ServerProperties.java @@ -0,0 +1,66 @@ +package com.baeldung.properties.yamlmap.pojo; + +import java.util.List; +import java.util.Map; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +import com.baeldung.properties.yamlmap.factory.YamlPropertySourceFactory; + +@Component +@PropertySource(value = "classpath:server.yml", factory = YamlPropertySourceFactory.class) +@ConfigurationProperties(prefix = "server") +public class ServerProperties { + + private Map application; + private Map> config; + private Map users; + + public Map getApplication() { + return application; + } + + public void setApplication(Map application) { + this.application = application; + } + + public Map> getConfig() { + return config; + } + + public void setConfig(Map> config) { + this.config = config; + } + + public Map getUsers() { + return users; + } + + public void setUsers(Map users) { + this.users = users; + } + + public static class Credential { + + private String username; + private String password; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/resources/server.yml b/spring-boot-modules/spring-boot-properties-2/src/main/resources/server.yml new file mode 100644 index 0000000000..51ba814a97 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/resources/server.yml @@ -0,0 +1,22 @@ +server: + application: + name: InjectMapFromYAML + url: http://injectmapfromyaml.dev + description: How To Inject a map from a YAML File in Spring Boot + config: + ips: + - 10.10.10.10 + - 10.10.10.11 + - 10.10.10.12 + - 10.10.10.13 + filesystem: + - /dev/root + - /dev/md2 + - /dev/md4 + users: + root: + username: root + password: rootpass + guest: + username: guest + password: guestpass diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamlmap/MapFromYamlIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamlmap/MapFromYamlIntegrationTest.java new file mode 100644 index 0000000000..b193e30417 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yamlmap/MapFromYamlIntegrationTest.java @@ -0,0 +1,43 @@ +package com.baeldung.properties.yamlmap; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.properties.yamlmap.pojo.ServerProperties; + +@RunWith(SpringRunner.class) +@SpringBootTest +class MapFromYamlIntegrationTest { + + @Autowired + private ServerProperties serverProperties; + + @Test + public void whenYamlFileProvidedThenInjectSimpleMap() { + + assertThat(serverProperties.getApplication()).containsOnlyKeys("name", "url", "description"); + + assertThat(serverProperties.getApplication() + .get("name")).isEqualTo("InjectMapFromYAML"); + } + + @Test + public void whenYamlFileProvidedThenInjectComplexMap() { + + assertThat(serverProperties.getConfig()).hasSize(2); + + assertThat(serverProperties.getConfig() + .get("ips") + .get(0)).isEqualTo("10.10.10.10"); + + assertThat(serverProperties.getUsers() + .get("root") + .getUsername()).isEqualTo("root"); + } + +}