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
This commit is contained in:
parent
3a99a52d11
commit
edd28c1b43
@ -1,13 +0,0 @@
|
|||||||
# Logs
|
|
||||||
logs
|
|
||||||
*.log
|
|
||||||
|
|
||||||
# Git
|
|
||||||
.git
|
|
||||||
.cache
|
|
||||||
|
|
||||||
# Classes
|
|
||||||
**/*.class
|
|
||||||
|
|
||||||
# Ignore md files
|
|
||||||
*.md
|
|
@ -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"]
|
|
@ -11,6 +11,9 @@ import org.springframework.boot.CommandLineRunner;
|
|||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
import com.baeldung.yaml.YAMLConfig.Idm;
|
||||||
|
import com.baeldung.yaml.YAMLConfig.Service;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class MyApplication implements CommandLineRunner {
|
public class MyApplication implements CommandLineRunner {
|
||||||
|
|
||||||
@ -26,6 +29,23 @@ public class MyApplication implements CommandLineRunner {
|
|||||||
System.out.println("using environment:" + myConfig.getEnvironment());
|
System.out.println("using environment:" + myConfig.getEnvironment());
|
||||||
System.out.println("name:" + myConfig.getName());
|
System.out.println("name:" + myConfig.getName());
|
||||||
System.out.println("servers:" + myConfig.getServers());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package com.baeldung.yaml;
|
package com.baeldung.yaml;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@ -13,6 +16,9 @@ public class YAMLConfig {
|
|||||||
private String name;
|
private String name;
|
||||||
private String environment;
|
private String environment;
|
||||||
private List<String> servers = new ArrayList<String>();
|
private List<String> servers = new ArrayList<String>();
|
||||||
|
private List<String> external = new ArrayList<String>();
|
||||||
|
private Map<String, String> map = new HashMap<String, String>();
|
||||||
|
private Component component = new Component();
|
||||||
|
|
||||||
public List<String> getServers() {
|
public List<String> getServers() {
|
||||||
return servers;
|
return servers;
|
||||||
@ -38,4 +44,110 @@ public class YAMLConfig {
|
|||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Component getComponent() {
|
||||||
|
return component;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setComponent(Component component) {
|
||||||
|
this.component = component;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getMap() {
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMap(Map<String, String> map) {
|
||||||
|
this.map = map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getExternal() {
|
||||||
|
return external;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExternal(List<String> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,26 @@ servers:
|
|||||||
- www.abc.test.com
|
- www.abc.test.com
|
||||||
- www.xyz.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:
|
spring:
|
||||||
|
@ -11,6 +11,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = MyApplication.class)
|
@SpringBootTest(classes = MyApplication.class)
|
||||||
|
@TestPropertySource(properties = {"spring.profiles.active = test"})
|
||||||
class YAMLIntegrationTest {
|
class YAMLIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -20,5 +21,6 @@ class YAMLIntegrationTest {
|
|||||||
void whenProfileTest_thenNameTesting() {
|
void whenProfileTest_thenNameTesting() {
|
||||||
assertTrue("testing".equalsIgnoreCase(config.getEnvironment()));
|
assertTrue("testing".equalsIgnoreCase(config.getEnvironment()));
|
||||||
assertTrue("test-YAML".equalsIgnoreCase(config.getName()));
|
assertTrue("test-YAML".equalsIgnoreCase(config.getName()));
|
||||||
|
assertTrue("myurl".equalsIgnoreCase(config.getComponent().getIdm().getUrl()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user