JAVA-18600 Update "Guide to @ConfigurationProperties in Spring Boot" … (#13670)

* JAVA-18600 Update "Guide to @ConfigurationProperties in Spring Boot" article

---------

Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
timis1 2023-04-13 16:18:11 +03:00 committed by GitHub
parent d964c3d1eb
commit fae3e80186
13 changed files with 73 additions and 62 deletions

View File

@ -63,10 +63,6 @@
<module>spring-boot-nashorn</module>
<module>spring-boot-parent</module>
<module>spring-boot-performance</module>
<module>spring-boot-properties</module>
<module>spring-boot-properties-2</module>
<module>spring-boot-properties-3</module>
<module>spring-boot-properties-migrator-demo</module>
<module>spring-boot-property-exp</module>
<module>spring-boot-request-params</module>
<module>spring-boot-runtime</module>
@ -94,6 +90,10 @@
<module>spring-boot-3-observation</module>
<module>spring-boot-3-test-pitfalls</module>
<module>spring-boot-resilience4j</module>
<module>spring-boot-properties</module>
<module>spring-boot-properties-2</module>
<module>spring-boot-properties-3</module>
<module>spring-boot-properties-migrator-demo</module>
</modules>
<dependencyManagement>

View File

@ -10,9 +10,10 @@
<description>Spring Boot Properties Module</description>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<dependencies>
@ -24,6 +25,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<properties>
<start-class>com.baeldung.properties.yaml.YamlApplication</start-class>
</properties>
</project>

View File

@ -7,11 +7,12 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import jakarta.annotation.PostConstruct;
@Configuration
@PropertySource(name = "myProperties", value = "values.properties")
public class ValuesApp {

View File

@ -1,6 +1,6 @@
package com.baeldung.properties.value.defaults;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -8,10 +8,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.Assert;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
import jakarta.annotation.PostConstruct;
/**
* Demonstrates setting defaults for @Value annotation. Note that there are no properties
* defined in the specified property source. We also assume that the user here
@ -60,10 +61,10 @@ public class ValuesWithDefaultsApp {
// arrays
List<String> stringListValues = Arrays.asList("one", "two", "three");
Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues), "unexpected value for stringArrayWithDefaults");
Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues), "unexpected value for stringArrayWithDefaults");
List<Integer> intListValues = Arrays.asList(1, 2, 3);
Assert.isTrue(Arrays.asList(ArrayUtils.toObject(intArrayWithDefaults)).containsAll(intListValues), "unexpected value for intArrayWithDefaults");
Assert.isTrue(Arrays.asList(ArrayUtils.toObject(intArrayWithDefaults)).containsAll(intListValues), "unexpected value for intArrayWithDefaults");
// SpEL
Assert.isTrue(spelWithDefaultValue.equals("my default system property value"), "unexpected value for spelWithDefaultValue");

View File

@ -9,9 +9,10 @@
<description>Spring Boot Properties Module</description>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<dependencies>
@ -31,7 +32,6 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
@ -44,4 +44,8 @@
</plugins>
</build>
<properties>
<start-class>com.baeldung.boot.properties.DemoApplication</start-class>
</properties>
</project>

View File

@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import jakarta.annotation.PostConstruct;
@Component
public class EnvironmentPropertiesPrinter {

View File

@ -7,10 +7,10 @@
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<!-- Used to demonstrate a particular scenario in the article -->
<!-- <parent> -->
@ -49,8 +49,8 @@
</build>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>

View File

@ -10,9 +10,10 @@
<description>Spring Boot Properties Module</description>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<dependencies>
@ -141,7 +142,7 @@
</profiles>
<properties>
<spring-cloud.version>2021.0.3</spring-cloud.version>
<spring-cloud.version>2022.0.1</spring-cloud.version>
<commons-configuration.version>1.10</commons-configuration.version>
<resource.delimiter>@</resource.delimiter>
<!-- <start-class>com.baeldung.buildproperties.Application</start-class> -->

View File

@ -3,10 +3,10 @@ package com.baeldung.configurationproperties;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length;
import org.springframework.boot.context.properties.ConfigurationProperties;

View File

@ -1,10 +1,8 @@
package com.baeldung.configurationproperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
@ConfigurationProperties(prefix = "mail.credentials")
@ConstructorBinding
public class ImmutableCredentials {
private final String authMethod;

View File

@ -4,12 +4,9 @@ import static org.junit.Assert.assertThat;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.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;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
class BuildInfoServiceIntegrationTest {

View File

@ -17,7 +17,7 @@ public class BuildPropertiesUnitTest {
@Test
void givenBuildPropertiesBean_WhenFetchDefaultBuildProperties_ThenGetValidValues() {
Assertions.assertEquals("spring-boot-properties", buildProperties.getArtifact());
Assertions.assertEquals("com.baeldung.spring-boot-modules", buildProperties.getGroup());
Assertions.assertEquals("com.baeldung", buildProperties.getGroup());
Assertions.assertEquals("0.0.1-SNAPSHOT", buildProperties.getVersion());
}

View File

@ -1,28 +1,27 @@
package com.baeldung.properties.reloading;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.baeldung.properties.reloading.beans.ConfigurationPropertiesRefreshConfigBean;
import com.baeldung.properties.reloading.beans.EnvironmentConfigBean;
import com.baeldung.properties.reloading.beans.PropertiesConfigBean;
import com.baeldung.properties.reloading.beans.ValueRefreshConfigBean;
import java.io.FileOutputStream;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootPropertiesTestApplication.class)
public class PropertiesReloadManualTest {
@ -50,7 +49,7 @@ public class PropertiesReloadManualTest {
ValueRefreshConfigBean singletonValueRefreshConfigBean;
@Before
@BeforeEach
public void setUp() throws Exception {
mvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
@ -61,7 +60,7 @@ public class PropertiesReloadManualTest {
callRefresh();
}
@After
@AfterEach
public void tearDown() throws Exception {
createConfig("extra.properties", "application.theme.color", "blue");
createConfig("extra2.properties", "application.theme.background", "red");
@ -69,76 +68,76 @@ public class PropertiesReloadManualTest {
@Test
public void givenEnvironmentReader_whenColorChanged_thenExpectChangeValue() throws Exception {
Assert.assertEquals("blue", environmentConfigBean.getColor());
assertEquals("blue", environmentConfigBean.getColor());
createConfig("extra.properties", "application.theme.color", "red");
Thread.sleep(refreshDelay);
Assert.assertEquals("red", environmentConfigBean.getColor());
assertEquals("red", environmentConfigBean.getColor());
}
@Test
public void givenEnvironmentReader_whenBackgroundChanged_thenExpectChangeValue() throws Exception {
Assert.assertEquals("red", environmentConfigBean.getBackgroundColor());
assertEquals("red", environmentConfigBean.getBackgroundColor());
createConfig("extra2.properties", "application.theme.background", "blue");
Thread.sleep(refreshDelay);
Assert.assertEquals("blue", environmentConfigBean.getBackgroundColor());
assertEquals("blue", environmentConfigBean.getBackgroundColor());
}
@Test
public void givenPropertiesReader_whenColorChanged_thenExpectChangeValue() throws Exception {
Assert.assertEquals("blue", propertiesConfigBean.getColor());
assertEquals("blue", propertiesConfigBean.getColor());
createConfig("extra.properties", "application.theme.color", "red");
Thread.sleep(refreshDelay);
Assert.assertEquals("red", propertiesConfigBean.getColor());
assertEquals("red", propertiesConfigBean.getColor());
}
@Test
public void givenRefreshScopedValueReader_whenColorChangedAndRefreshCalled_thenExpectChangeValue() throws Exception {
Assert.assertEquals("blue", valueRefreshConfigBean.getColor());
assertEquals("blue", valueRefreshConfigBean.getColor());
createConfig("extra.properties", "application.theme.color", "red");
Thread.sleep(refreshDelay);
Assert.assertEquals("blue", valueRefreshConfigBean.getColor());
assertEquals("blue", valueRefreshConfigBean.getColor());
callRefresh();
Assert.assertEquals("red", valueRefreshConfigBean.getColor());
assertEquals("red", valueRefreshConfigBean.getColor());
}
@Test
public void givenSingletonRefreshScopedValueReader_whenColorChangedAndRefreshCalled_thenExpectOldValue() throws Exception {
Assert.assertEquals("blue", singletonValueRefreshConfigBean.getColor());
assertEquals("blue", singletonValueRefreshConfigBean.getColor());
createConfig("extra.properties", "application.theme.color", "red");
Thread.sleep(refreshDelay);
Assert.assertEquals("blue", singletonValueRefreshConfigBean.getColor());
assertEquals("blue", singletonValueRefreshConfigBean.getColor());
callRefresh();
Assert.assertEquals("blue", singletonValueRefreshConfigBean.getColor());
assertEquals("blue", singletonValueRefreshConfigBean.getColor());
}
@Test
public void givenRefreshScopedConfigurationPropertiesReader_whenColorChangedAndRefreshCalled_thenExpectChangeValue() throws Exception {
Assert.assertEquals("blue", configurationPropertiesRefreshConfigBean.getColor());
assertEquals("blue", configurationPropertiesRefreshConfigBean.getColor());
createConfig("extra.properties", "application.theme.color", "red");
Thread.sleep(refreshDelay);
Assert.assertEquals("blue", configurationPropertiesRefreshConfigBean.getColor());
assertEquals("blue", configurationPropertiesRefreshConfigBean.getColor());
callRefresh();
Assert.assertEquals("red", configurationPropertiesRefreshConfigBean.getColor());
assertEquals("red", configurationPropertiesRefreshConfigBean.getColor());
}
public void callRefresh() throws Exception {
@ -148,7 +147,7 @@ public class PropertiesReloadManualTest {
.accept(MediaType.APPLICATION_JSON_VALUE))
.andReturn();
MockHttpServletResponse response = mvcResult.getResponse();
Assert.assertEquals(response.getStatus(), 200);
assertEquals(200, response.getStatus());
}
public void createConfig(String file, String key, String value) throws Exception {