BAEL-2855 Add a new section in ConfigurationProperties article (#6808)
This commit is contained in:
parent
230725102b
commit
d5ba7790ef
@ -9,6 +9,7 @@ import javax.validation.constraints.NotBlank;
|
|||||||
import javax.validation.constraints.Pattern;
|
import javax.validation.constraints.Pattern;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
@ -80,4 +81,10 @@ public class ConfigProperties {
|
|||||||
public void setCredentials(Credentials credentials) {
|
public void setCredentials(Credentials credentials) {
|
||||||
this.credentials = credentials;
|
this.credentials = credentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConfigurationProperties(prefix = "item")
|
||||||
|
public Item item(){
|
||||||
|
return new Item();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
31
spring-boot/src/main/java/org/baeldung/properties/Item.java
Normal file
31
spring-boot/src/main/java/org/baeldung/properties/Item.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -17,4 +17,8 @@ mail.credentials.username=john
|
|||||||
mail.credentials.password=password
|
mail.credentials.password=password
|
||||||
mail.credentials.authMethod=SHA1
|
mail.credentials.authMethod=SHA1
|
||||||
|
|
||||||
|
#Bean method properties
|
||||||
|
item.name=Item name
|
||||||
|
item.size=42
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,4 +53,11 @@ public class ConfigPropertiesIntegrationTest {
|
|||||||
Assert.assertEquals("Incorrectly bound object property, username", "john", credentials.getUsername());
|
Assert.assertEquals("Incorrectly bound object property, username", "john", credentials.getUsername());
|
||||||
Assert.assertEquals("Incorrectly bound object property, password", "password", credentials.getPassword());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,3 +17,6 @@ mail.credentials.username=john
|
|||||||
mail.credentials.password=password
|
mail.credentials.password=password
|
||||||
mail.credentials.authMethod=SHA1
|
mail.credentials.authMethod=SHA1
|
||||||
|
|
||||||
|
#Bean method properties
|
||||||
|
item.name=Test item name
|
||||||
|
item.size=21
|
||||||
|
Loading…
x
Reference in New Issue
Block a user