From 37bf5fc677c384579559eee25cb2e5e599bb27d3 Mon Sep 17 00:00:00 2001 From: Carlos Cavero Date: Fri, 12 Jun 2020 19:40:14 +0200 Subject: [PATCH] BAEL-3756-Spring-YAML-vs-Properties (#9482) * Remove Dockerfile because it is not longer needed in the article * Add different YAML configurations and process into POJO * Remove .dockerignore file because it is not longer needed in the article * Add default Spring profile to test and force also in the Unit tests --- .../spring-boot-properties/.dockerignore | 13 -- .../spring-boot-properties/Dockerfile | 10 -- .../java/com/baeldung/yaml/MyApplication.java | 20 ++++ .../java/com/baeldung/yaml/YAMLConfig.java | 112 ++++++++++++++++++ .../src/main/resources/application.yml | 34 ++++-- .../baeldung/yaml/YAMLIntegrationTest.java | 2 + 6 files changed, 161 insertions(+), 30 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-properties/.dockerignore delete mode 100644 spring-boot-modules/spring-boot-properties/Dockerfile diff --git a/spring-boot-modules/spring-boot-properties/.dockerignore b/spring-boot-modules/spring-boot-properties/.dockerignore deleted file mode 100644 index df36044e46..0000000000 --- a/spring-boot-modules/spring-boot-properties/.dockerignore +++ /dev/null @@ -1,13 +0,0 @@ -# Logs -logs -*.log - -# Git -.git -.cache - -# Classes -**/*.class - -# Ignore md files -*.md diff --git a/spring-boot-modules/spring-boot-properties/Dockerfile b/spring-boot-modules/spring-boot-properties/Dockerfile deleted file mode 100644 index d6bd2a95ae..0000000000 --- a/spring-boot-modules/spring-boot-properties/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM maven:3.6.0-jdk-11 -WORKDIR /code/spring-boot-modules/spring-boot-properties/ -COPY ./spring-boot-modules/spring-boot-properties/pom.xml . -COPY ./spring-boot-modules/spring-boot-properties/src ./src -COPY ./parent-boot-2/pom.xml /code/parent-boot-2/pom.xml -COPY ./pom.xml /code/pom.xml -COPY ./custom-pmd-0.0.1.jar /code/custom-pmd-0.0.1.jar -COPY ./baeldung-pmd-rules.xml /code/baeldung-pmd-rules.xml -RUN mvn dependency:resolve -CMD ["mvn", "spring-boot:run"] \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/MyApplication.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/MyApplication.java index d42b731213..f3cfff57b7 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/MyApplication.java +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/MyApplication.java @@ -11,6 +11,9 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import com.baeldung.yaml.YAMLConfig.Idm; +import com.baeldung.yaml.YAMLConfig.Service; + @SpringBootApplication public class MyApplication implements CommandLineRunner { @@ -26,6 +29,23 @@ public class MyApplication implements CommandLineRunner { System.out.println("using environment:" + myConfig.getEnvironment()); System.out.println("name:" + myConfig.getName()); System.out.println("servers:" + myConfig.getServers()); + + if ("testing".equalsIgnoreCase(myConfig.getEnvironment())) { + System.out.println("external:" + myConfig.getExternal()); + System.out.println("map:" + myConfig.getMap()); + + Idm idm = myConfig.getComponent().getIdm(); + Service service = myConfig.getComponent().getService(); + System.out.println("Idm:"); + System.out.println(" Url: " + idm.getUrl()); + System.out.println(" User: " + idm.getUser()); + System.out.println(" Password: " + idm.getPassword()); + System.out.println(" Description: " + idm.getDescription()); + System.out.println("Service:"); + System.out.println(" Url: " + service.getUrl()); + System.out.println(" Token: " + service.getToken()); + System.out.println(" Description: " + service.getDescription()); + } } } diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/YAMLConfig.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/YAMLConfig.java index ad633c4b56..83c083734c 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/YAMLConfig.java +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/YAMLConfig.java @@ -1,7 +1,10 @@ package com.baeldung.yaml; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; + import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; @@ -13,6 +16,9 @@ public class YAMLConfig { private String name; private String environment; private List servers = new ArrayList(); + private List external = new ArrayList(); + private Map map = new HashMap(); + private Component component = new Component(); public List getServers() { return servers; @@ -37,5 +43,111 @@ public class YAMLConfig { public void setEnvironment(String environment) { this.environment = environment; } + + public Component getComponent() { + return component; + } + + public void setComponent(Component component) { + this.component = component; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } + + public List getExternal() { + return external; + } + + public void setExternal(List external) { + this.external = external; + } + + public class Component { + private Idm idm = new Idm(); + private Service service = new Service(); + + public Idm getIdm() { + return idm; + } + public void setIdm(Idm idm) { + this.idm = idm; + } + + public Service getService() { + return service; + } + public void setService(Service service) { + this.service = service; + } + + } + + public class Idm { + private String url; + private String user; + private String password; + private String description; + + public String getUrl() { + return url; + } + public void setUrl(String url) { + this.url = url; + } + + public String getUser() { + return user; + } + public void setUser(String user) { + this.user = user; + } + + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + } + + public class Service { + private String url; + private String token; + private String description; + + public String getUrl() { + return url; + } + public void setUrl(String url) { + this.url = url; + } + + public String getToken() { + return token; + } + public void setToken(String token) { + this.token = token; + } + + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + } } diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml b/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml index 4914ff15f7..30e64f9d35 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml @@ -6,22 +6,42 @@ spring: --- spring: - profiles: test + profiles: test name: test-YAML environment: testing servers: - - www.abc.test.com - - www.xyz.test.com - + - www.abc.test.com + - www.xyz.test.com + +external: [www.abc.test.com, www.xyz.test.com] + +map: + firstkey: key1 + secondkey: key2 + +component: + idm: + url: myurl + user: user + password: password + description: > + this should be a long + description + service: + url: myurlservice + token: token + description: > + this should be another long + description --- spring: - profiles: prod + profiles: prod name: prod-YAML environment: production servers: - - www.abc.com - - www.xyz.com + - www.abc.com + - www.xyz.com --- diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java index 090d5c592e..19412c91f5 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java @@ -11,6 +11,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = MyApplication.class) +@TestPropertySource(properties = {"spring.profiles.active = test"}) class YAMLIntegrationTest { @Autowired @@ -20,5 +21,6 @@ class YAMLIntegrationTest { void whenProfileTest_thenNameTesting() { assertTrue("testing".equalsIgnoreCase(config.getEnvironment())); assertTrue("test-YAML".equalsIgnoreCase(config.getName())); + assertTrue("myurl".equalsIgnoreCase(config.getComponent().getIdm().getUrl())); } }