BAEL-2926 Guide to @EnableConfigurationProperties (#7186)

This commit is contained in:
kwoyke 2019-06-25 06:07:26 +02:00 committed by Grzegorz Piwowarek
parent 49bafb0300
commit 8470ca7c5b
5 changed files with 59 additions and 2 deletions

View File

@ -0,0 +1,14 @@
package org.baeldung.properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(AdditionalProperties.class)
public class AdditionalConfiguration {
@Autowired
private AdditionalProperties additionalProperties;
}

View File

@ -0,0 +1,26 @@
package org.baeldung.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "additional")
public class AdditionalProperties {
private String unit;
private int max;
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
}

View File

@ -5,7 +5,10 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
@ComponentScan(basePackageClasses = {ConfigProperties.class,
JsonProperties.class,
CustomJsonProperties.class,
AdditionalConfiguration.class})
public class ConfigPropertiesDemoApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())

View File

@ -1,5 +1,6 @@
package com.baeldung.properties;
import org.baeldung.properties.AdditionalProperties;
import org.baeldung.properties.ConfigProperties;
import org.baeldung.properties.ConfigPropertiesDemoApplication;
import org.junit.Assert;
@ -18,6 +19,9 @@ public class ConfigPropertiesIntegrationTest {
@Autowired
private ConfigProperties properties;
@Autowired
private AdditionalProperties additionalProperties;
@Test
public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception {
Assert.assertTrue("From address is read as null!", properties.getFrom() != null);
@ -42,4 +46,10 @@ public class ConfigPropertiesIntegrationTest {
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getUsername().equals("john"));
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getPassword().equals("password"));
}
@Test
public void whenAdditionalPropertyQueriedthenReturnsProperty() {
Assert.assertTrue(additionalProperties.getUnit().equals("km"));
Assert.assertTrue(additionalProperties.getMax() == 100);
}
}

View File

@ -20,3 +20,7 @@ mail.credentials.authMethod=SHA1
#Bean method properties
item.name=Test item name
item.size=21
#Additional properties
additional.unit=km
additional.max=100