improved and updated spring-boot-yaml-vs-properties article code

This commit is contained in:
Gerardo Roza 2021-01-11 14:23:27 -03:00
parent 7347c8fe16
commit 0de3237365
8 changed files with 120 additions and 14 deletions

View File

@ -0,0 +1,4 @@
spring.datasource.password: 'password'
spring.datasource.url: jdbc:mysql://localhost:3306/db_integration
spring.datasource.username: user
bael.property=integrationValue

View File

@ -9,7 +9,17 @@ spring.datasource.username=SA
spring.datasource.password=password spring.datasource.password=password
app.name=MyApp app.name=MyApp
app.description=${app.name} is a Spring Boot application app.description=${app.name} is a Spring Boot application
logging.file.name=myapplication.log
bael.property=defaultValue bael.property=defaultValue
#--- #---
spring.config.activate.on-profile=prod spring.config.activate.on-profile=multidocument-dev
spring.datasource.password=password
spring.datasource.url=jdbc:h2:dev
spring.datasource.username=SA
bael.property=devValue
#---
spring.config.activate.on-profile=multidocument-prod
spring.datasource.password=password
spring.datasource.url=jdbc:h2:prod
spring.datasource.username=prodUser
bael.property=prodValue bael.property=prodValue

View File

@ -1,17 +1,17 @@
logging: bael:
file: root-level-property: defaultRootLevelValue
name: myapplication.log
spring:
datasource:
password: 'password'
url: jdbc:h2:dev
username: SA
--- ---
spring: spring:
config:
activate:
on-profile: multidocument-staging
datasource: datasource:
password: 'password' password: 'password'
url: jdbc:mysql://localhost:3306/db_production url: jdbc:h2:staging
username: user username: SA
bael:
property: stagingValue
---
application: application:
servers: servers:
- ip: '127.0.0.1' - ip: '127.0.0.1'

View File

@ -10,13 +10,17 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import com.baeldung.boot.properties.DemoApplication; import com.baeldung.boot.properties.DemoApplication;
@SpringBootTest(classes = { DemoApplication.class }, webEnvironment = WebEnvironment.MOCK) @SpringBootTest(classes = { DemoApplication.class }, webEnvironment = WebEnvironment.MOCK)
public class DefaultMultidocumentPropertiesFileIntegrationTest { public class DefaultMultidocumentFilesIntegrationTest {
@Value("${bael.property}") @Value("${bael.property}")
private String baelCustomProperty; private String baelCustomProperty;
@Value("${bael.root-level-property}")
private String baelRootProperty;
@Test @Test
public void givenDefaultProfileActive_whenApplicationStarts_thenDefaultPropertiesUser() { public void givenDefaultProfileActive_whenApplicationStarts_thenDefaultPropertiesUser() {
assertThat(baelCustomProperty).isEqualTo("defaultValue"); assertThat(baelCustomProperty).isEqualTo("defaultValue");
assertThat(baelRootProperty).isEqualTo("defaultRootLevelValue");
} }
} }

View File

@ -0,0 +1,28 @@
package com.baeldung.boot.properties.multidocument;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.ActiveProfiles;
import com.baeldung.boot.properties.DemoApplication;
@SpringBootTest(classes = { DemoApplication.class }, webEnvironment = WebEnvironment.MOCK)
@ActiveProfiles("multidocument-dev")
public class DevMultidocumentFilesIntegrationTest {
@Value("${bael.property}")
private String baelCustomProperty;
@Value("${bael.root-level-property}")
private String baelRootProperty;
@Test
public void givenDefaultProfileActive_whenApplicationStarts_thenDefaultPropertiesUser() {
assertThat(baelCustomProperty).isEqualTo("devValue");
assertThat(baelRootProperty).isEqualTo("defaultRootLevelValue");
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.boot.properties.multidocument;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.ActiveProfiles;
import com.baeldung.boot.properties.DemoApplication;
@SpringBootTest(classes = { DemoApplication.class }, webEnvironment = WebEnvironment.MOCK)
@ActiveProfiles("multidocument-integration")
public class IntegrationMultidocumentFilesIntegrationTest {
@Value("${bael.property}")
private String baelCustomProperty;
@Value("${bael.root-level-property}")
private String baelRootProperty;
@Test
public void givenProductionProfileActive_whenApplicationStarts_thenDefaultPropertiesUser() {
assertThat(baelCustomProperty).isEqualTo("integrationValue");
assertThat(baelRootProperty).isEqualTo("defaultRootLevelValue");
}
}

View File

@ -11,14 +11,18 @@ import org.springframework.test.context.ActiveProfiles;
import com.baeldung.boot.properties.DemoApplication; import com.baeldung.boot.properties.DemoApplication;
@SpringBootTest(classes = { DemoApplication.class }, webEnvironment = WebEnvironment.MOCK) @SpringBootTest(classes = { DemoApplication.class }, webEnvironment = WebEnvironment.MOCK)
@ActiveProfiles("prod") @ActiveProfiles("multidocument-prod")
public class ProdMultidocumentPropertiesFileIntegrationTest { public class ProdMultidocumentFilesIntegrationTest {
@Value("${bael.property}") @Value("${bael.property}")
private String baelCustomProperty; private String baelCustomProperty;
@Value("${bael.root-level-property}")
private String baelRootProperty;
@Test @Test
public void givenProductionProfileActive_whenApplicationStarts_thenDefaultPropertiesUser() { public void givenProductionProfileActive_whenApplicationStarts_thenDefaultPropertiesUser() {
assertThat(baelCustomProperty).isEqualTo("prodValue"); assertThat(baelCustomProperty).isEqualTo("prodValue");
assertThat(baelRootProperty).isEqualTo("defaultRootLevelValue");
} }
} }

View File

@ -0,0 +1,28 @@
package com.baeldung.boot.properties.multidocument;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.ActiveProfiles;
import com.baeldung.boot.properties.DemoApplication;
@SpringBootTest(classes = { DemoApplication.class }, webEnvironment = WebEnvironment.MOCK)
@ActiveProfiles("multidocument-staging")
public class StagingMultidocumentFilesIntegrationTest {
@Value("${bael.property}")
private String baelCustomProperty;
@Value("${bael.root-level-property}")
private String baelRootProperty;
@Test
public void givenProductionProfileActive_whenApplicationStarts_thenDefaultPropertiesUser() {
assertThat(baelCustomProperty).isEqualTo("stagingValue");
assertThat(baelRootProperty).isEqualTo("defaultRootLevelValue");
}
}