From 16ae922c36fdfbf2a47f22cfd1f65fa032461313 Mon Sep 17 00:00:00 2001 From: Ricardo Caldas Date: Sat, 22 Aug 2020 05:45:22 -0300 Subject: [PATCH] BAEL-4106 (#9887) * BAEL-4106 application.yml vs application.properties for Spring Boot * Update README.md --- .../spring-boot-properties-3/README.md | 6 +++++ .../spring-boot-properties-3/pom.xml | 3 --- .../propertiesvsyaml/ConfigProperties.java | 27 +++++++++++++++++++ .../EnvironmentProperties.java | 16 +++++++++++ .../propertiesvsyaml/ValueProperties.java | 13 +++++++++ .../src/main/resources/application.properties | 11 ++++++++ .../src/main/resources/application.yml | 22 +++++++++++++++ 7 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/ConfigProperties.java create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/EnvironmentProperties.java create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/ValueProperties.java create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/resources/application.properties diff --git a/spring-boot-modules/spring-boot-properties-3/README.md b/spring-boot-modules/spring-boot-properties-3/README.md index df6ba5480a..d89f825c86 100644 --- a/spring-boot-modules/spring-boot-properties-3/README.md +++ b/spring-boot-modules/spring-boot-properties-3/README.md @@ -1,3 +1,9 @@ + +## Spring Boot Properties + + + ### Relevant Articles: - [How to Define a Map in YAML for a POJO?](https://www.baeldung.com/yaml-map-pojo) + diff --git a/spring-boot-modules/spring-boot-properties-3/pom.xml b/spring-boot-modules/spring-boot-properties-3/pom.xml index cf94e1fc1d..44e2ef52ac 100644 --- a/spring-boot-modules/spring-boot-properties-3/pom.xml +++ b/spring-boot-modules/spring-boot-properties-3/pom.xml @@ -17,13 +17,11 @@ 1.8 - org.springframework.boot spring-boot-starter - org.springframework.boot spring-boot-starter-test @@ -50,5 +48,4 @@ - diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/ConfigProperties.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/ConfigProperties.java new file mode 100644 index 0000000000..3a7ed76276 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/ConfigProperties.java @@ -0,0 +1,27 @@ +package com.baeldung.propertiesvsyaml; + +import org.springframework.boot.context.properties.ConfigurationProperties; + + +@ConfigurationProperties(prefix = "app") +public class ConfigProperties { + + String name; + String description; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/EnvironmentProperties.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/EnvironmentProperties.java new file mode 100644 index 0000000000..b5586956cc --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/EnvironmentProperties.java @@ -0,0 +1,16 @@ +package com.baeldung.propertiesvsyaml; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +public class EnvironmentProperties { + + @Autowired + private Environment env; + + public String getSomeKey() { + return env.getProperty("key.something"); + } +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/ValueProperties.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/ValueProperties.java new file mode 100644 index 0000000000..fb9ef9b1cf --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/propertiesvsyaml/ValueProperties.java @@ -0,0 +1,13 @@ +package com.baeldung.propertiesvsyaml; + +import org.springframework.beans.factory.annotation.Value; + +public class ValueProperties { + + @Value("${key.something}") + private String injectedProperty; + + public String getAppName() { + return injectedProperty; + } +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.properties b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.properties new file mode 100644 index 0000000000..eace1f0e46 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.properties @@ -0,0 +1,11 @@ +application.servers[0].ip=127.0.0.1 +application.servers[0].path=/path1 +application.servers[1].ip=127.0.0.2 +application.servers[1].path=/path2 +application.servers[2].ip=127.0.0.3 +application.servers[2].path=/path3 +spring.datasource.url=jdbc:h2:dev +spring.datasource.username=SA +spring.datasource.password=password +app.name=MyApp +app.description=${app.name} is a Spring Boot application \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml index 8779cb6b0c..00baeade9c 100644 --- a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml @@ -1,3 +1,25 @@ +logging: + file: + name: myapplication.log +spring: + datasource: + password: 'password' + url: jdbc:h2:dev + username: SA +--- +spring: + datasource: + password: 'password' + url: jdbc:mysql://localhost:3306/db_production + username: user +application: + servers: + - ip: '127.0.0.1' + path: '/path1' + - ip: '127.0.0.2' + path: '/path2' + - ip: '127.0.0.3' + path: '/path3' t-shirt-size: simple-mapping: XS: 6