BAEL-2926 Guide to @EnableConfigurationProperties (#7186)
This commit is contained in:
parent
49bafb0300
commit
8470ca7c5b
|
@ -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;
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -5,11 +5,14 @@ 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())
|
||||
.run();
|
||||
.run();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue