Skip to content

Commit ae8efcd

Browse files
committed
editorial review
1 parent 9ab810a commit ae8efcd

7 files changed

Lines changed: 18 additions & 26 deletions

File tree

‎spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.baeldung.properties;
22

3-
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
44
import org.springframework.boot.builder.SpringApplicationBuilder;
55
import org.springframework.context.annotation.ComponentScan;
66

7-
@EnableAutoConfiguration
7+
@SpringBootApplication
88
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
99
public class ConfigPropertiesDemoApplication {
1010
public static void main(String[] args) {

‎spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.baeldung.properties;
22

33
import org.springframework.boot.context.properties.ConfigurationProperties;
4-
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.stereotype.Component;
55

6-
@Configuration
6+
@Component
77
@ConfigurationProperties(prefix = "custom")
88
public class CustomJsonProperties {
99

‎spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import java.util.List;
55

66
import org.springframework.boot.context.properties.ConfigurationProperties;
7-
import org.springframework.context.annotation.Configuration;
87
import org.springframework.context.annotation.PropertySource;
8+
import org.springframework.stereotype.Component;
99

10-
@Configuration
10+
@Component
1111
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
1212
@ConfigurationProperties
1313
public class JsonProperties {

‎spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class JsonPropertyContextInitializer implements ApplicationContextInitial
2626
@SuppressWarnings("unchecked")
2727
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
2828
try {
29-
Resource resource = configurableApplicationContext.getResource("classpath:configpropscustom.json");
29+
Resource resource = configurableApplicationContext.getResource("classpath:configprops.json");
3030
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
3131
Set<Map.Entry> set = readValue.entrySet();
3232
List<MapPropertySource> propertySources = convertEntrySet(set, Optional.empty());

‎spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Map;
55

66
import org.springframework.core.env.MapPropertySource;
7+
import org.springframework.core.env.PropertySource;
78
import org.springframework.core.io.support.EncodedResource;
89
import org.springframework.core.io.support.PropertySourceFactory;
910

@@ -12,7 +13,7 @@
1213
public class JsonPropertySourceFactory implements PropertySourceFactory {
1314

1415
@Override
15-
public org.springframework.core.env.PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
16+
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
1617
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
1718
return new MapPropertySource("json-property", readValue);
1819
}

‎spring-boot/src/main/resources/configpropscustom.json‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,38 @@ public class JsonPropertiesIntegrationTest {
2121
private CustomJsonProperties customJsonProperties;
2222

2323
@Test
24-
public void givenJsonPropertySource_whenPropertySourceFactoryUsed_thenLoadFlatValues() {
24+
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() {
2525
Assert.assertEquals("mailer@mail.com", jsonProperties.getHost());
2626
Assert.assertEquals(9090, jsonProperties.getPort());
2727
Assert.assertTrue(jsonProperties.isResend());
2828
}
2929

3030
@Test
31-
public void givenJsonPropertySource_whenPropertySourceFactoryUsed_thenLoadListValues() {
31+
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() {
3232
Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot")));
3333
}
3434

3535
@Test
36-
public void givenJsonPropertySource_whenPropertySourceFactoryUsed_thenNestedValuesLoadedAsMap() {
36+
public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() {
3737
Assert.assertEquals("sender", jsonProperties.getSender()
3838
.get("name"));
3939
Assert.assertEquals("street", jsonProperties.getSender()
4040
.get("address"));
4141
}
4242

4343
@Test
44-
public void givenCustomJsonPropertySource_whenLoadedIntoEnvironment_thenFlatValuesPopulated() {
45-
Assert.assertEquals("mailer.custom@mail.com", customJsonProperties.getHost());
46-
Assert.assertEquals(8081, customJsonProperties.getPort());
44+
public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() {
45+
Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost());
46+
Assert.assertEquals(9090, customJsonProperties.getPort());
4747
Assert.assertTrue(customJsonProperties.isResend());
4848
}
4949

5050
@Test
51-
public void givenCustomJsonPropertySource_whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() {
51+
public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() {
5252
Assert.assertNotNull(customJsonProperties.getSender());
53-
Assert.assertEquals("sender.custom", customJsonProperties.getSender()
53+
Assert.assertEquals("sender", customJsonProperties.getSender()
5454
.getName());
55-
Assert.assertEquals("street.custom", customJsonProperties.getSender()
55+
Assert.assertEquals("street", customJsonProperties.getSender()
5656
.getAddress());
5757
}
5858

0 commit comments

Comments
 (0)