Skip to content

Commit d5ba779

Browse files
kwoykepivovarit
authored andcommitted
BAEL-2855 Add a new section in ConfigurationProperties article (eugenp#6808)
1 parent 2307251 commit d5ba779

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import javax.validation.constraints.Pattern;
1010

1111
import org.springframework.boot.context.properties.ConfigurationProperties;
12+
import org.springframework.context.annotation.Bean;
1213
import org.springframework.context.annotation.Configuration;
1314
import org.springframework.context.annotation.PropertySource;
1415
import org.springframework.validation.annotation.Validated;
@@ -80,4 +81,10 @@ public Credentials getCredentials() {
8081
public void setCredentials(Credentials credentials) {
8182
this.credentials = credentials;
8283
}
84+
85+
@Bean
86+
@ConfigurationProperties(prefix = "item")
87+
public Item item(){
88+
return new Item();
89+
}
8390
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.baeldung.properties;
2+
3+
public class Item {
4+
5+
private String name;
6+
private int size;
7+
8+
public Item() {
9+
}
10+
11+
public Item(String name, int size) {
12+
this.name = name;
13+
this.size = size;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public int getSize() {
25+
return size;
26+
}
27+
28+
public void setSize(int size) {
29+
this.size = size;
30+
}
31+
}

spring-boot/src/main/resources/configprops.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ mail.credentials.username=john
1717
mail.credentials.password=password
1818
mail.credentials.authMethod=SHA1
1919

20+
#Bean method properties
21+
item.name=Item name
22+
item.size=42
23+
2024

spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@ public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception {
5353
Assert.assertEquals("Incorrectly bound object property, username", "john", credentials.getUsername());
5454
Assert.assertEquals("Incorrectly bound object property, password", "password", credentials.getPassword());
5555
}
56+
57+
@Test
58+
public void whenBeanMethodAnnotatedThenPropertiesCorrectlyBound(){
59+
Item item = properties.item();
60+
Assert.assertEquals("Incorrectly bound object property, item.name","Test item name", item.getName());
61+
Assert.assertEquals("Incorrectly bound object property, item.size", 21, item.getSize());
62+
}
5663
}

spring-boot/src/test/resources/configprops-test.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ mail.credentials.username=john
1717
mail.credentials.password=password
1818
mail.credentials.authMethod=SHA1
1919

20+
#Bean method properties
21+
item.name=Test item name
22+
item.size=21

0 commit comments

Comments
 (0)