diff --git a/spring-boot-modules/spring-boot-properties-2/README.md b/spring-boot-modules/spring-boot-properties-2/README.md new file mode 100644 index 0000000000..01e2970e89 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/README.md @@ -0,0 +1,5 @@ +## Spring Boot Properties + +This module contains articles about Properties in Spring Boot. + +### Relevant Articles: diff --git a/spring-boot-modules/spring-boot-properties-2/pom.xml b/spring-boot-modules/spring-boot-properties-2/pom.xml new file mode 100644 index 0000000000..c3c3e57251 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + spring-boot-properties-2 + spring-boot-properties-2 + jar + Spring Boot Properties Module + 0.0.1-SNAPSHOT + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter + + + + diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlApplication.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlApplication.java new file mode 100644 index 0000000000..f1244925f1 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlApplication.java @@ -0,0 +1,22 @@ +package com.baeldung.properties.yaml; + +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 YamlApplication implements CommandLineRunner { + + @Autowired + private YamlFooProperties yamlFooProperties; + + public static void main(String[] args) { + SpringApplication.run(YamlApplication.class, args); + } + + @Override + public void run(String... args) throws Exception { + System.out.println("YAML Properties " + yamlFooProperties); + } +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlFooProperties.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlFooProperties.java new file mode 100644 index 0000000000..264fc865ad --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlFooProperties.java @@ -0,0 +1,42 @@ +package com.baeldung.properties.yaml; + +import com.baeldung.properties.yaml.factory.YamlPropertySourceFactory; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +import java.util.List; + +@Configuration +@ConfigurationProperties(prefix = "yaml") +@PropertySource(value = "classpath:foo.yml", factory = YamlPropertySourceFactory.class) +public class YamlFooProperties { + + private String name; + + private List aliases; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getAliases() { + return aliases; + } + + public void setAliases(List aliases) { + this.aliases = aliases; + } + + @Override + public String toString() { + return "YamlFooProperties{" + + "name='" + name + '\'' + + ", aliases=" + aliases + + '}'; + } +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/factory/YamlPropertySourceFactory.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/factory/YamlPropertySourceFactory.java new file mode 100644 index 0000000000..50201f9377 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/factory/YamlPropertySourceFactory.java @@ -0,0 +1,24 @@ +package com.baeldung.properties.yaml.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/resources/foo.yml b/spring-boot-modules/spring-boot-properties-2/src/main/resources/foo.yml new file mode 100644 index 0000000000..6320510a94 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/resources/foo.yml @@ -0,0 +1,5 @@ +yaml: + name: foo + aliases: + - abc + - xyz \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yaml/YamlFooPropertiesIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yaml/YamlFooPropertiesIntegrationTest.java new file mode 100644 index 0000000000..dfe1c830b4 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yaml/YamlFooPropertiesIntegrationTest.java @@ -0,0 +1,23 @@ +package com.baeldung.properties.yaml; + +import org.junit.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 static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class YamlFooPropertiesIntegrationTest { + + @Autowired + private YamlFooProperties yamlFooProperties; + + @Test + public void whenFactoryProvidedThenYamlPropertiesInjected() { + assertThat(yamlFooProperties.getName()).isEqualTo("foo"); + assertThat(yamlFooProperties.getAliases()).containsExactly("abc", "xyz"); + } +} \ No newline at end of file