BAEL-2855 Add a new section in ConfigurationProperties article (#6808)

This commit is contained in:
kwoyke 2019-04-24 20:02:04 +02:00 committed by Grzegorz Piwowarek
parent 230725102b
commit d5ba7790ef
5 changed files with 52 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.validation.annotation.Validated;
@ -80,4 +81,10 @@ public class ConfigProperties {
public void setCredentials(Credentials credentials) {
this.credentials = credentials;
}
@Bean
@ConfigurationProperties(prefix = "item")
public Item item(){
return new Item();
}
}

View File

@ -0,0 +1,31 @@
package org.baeldung.properties;
public class Item {
private String name;
private int size;
public Item() {
}
public Item(String name, int size) {
this.name = name;
this.size = size;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}

View File

@ -17,4 +17,8 @@ mail.credentials.username=john
mail.credentials.password=password
mail.credentials.authMethod=SHA1
#Bean method properties
item.name=Item name
item.size=42

View File

@ -53,4 +53,11 @@ public class ConfigPropertiesIntegrationTest {
Assert.assertEquals("Incorrectly bound object property, username", "john", credentials.getUsername());
Assert.assertEquals("Incorrectly bound object property, password", "password", credentials.getPassword());
}
@Test
public void whenBeanMethodAnnotatedThenPropertiesCorrectlyBound(){
Item item = properties.item();
Assert.assertEquals("Incorrectly bound object property, item.name","Test item name", item.getName());
Assert.assertEquals("Incorrectly bound object property, item.size", 21, item.getSize());
}
}

View File

@ -17,3 +17,6 @@ mail.credentials.username=john
mail.credentials.password=password
mail.credentials.authMethod=SHA1
#Bean method properties
item.name=Test item name
item.size=21