added code examples for @ConfigurationProperties
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package io.reflectoring.configuration;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ConfigurationApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConfigurationApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.reflectoring.configuration.mail;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(MailModuleProperties.class)
|
||||
class MailModuleConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConfigurationPropertiesBinding
|
||||
public WeightConverter weightConverter() {
|
||||
return new WeightConverter();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package io.reflectoring.configuration.mail;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@ConfigurationProperties(prefix = "myapp.mail", ignoreInvalidFields = true, ignoreUnknownFields = false)
|
||||
@Validated
|
||||
class MailModuleProperties {
|
||||
|
||||
@NotNull
|
||||
private Boolean enabled = Boolean.TRUE;
|
||||
|
||||
@NotEmpty
|
||||
private String defaultSubject;
|
||||
|
||||
@NotNull
|
||||
private Duration pauseBetweenMails;
|
||||
|
||||
@NotNull
|
||||
private DataSize maxAttachmentSize;
|
||||
|
||||
@NotNull
|
||||
private Weight maxAttachmentWeight;
|
||||
|
||||
@NotEmpty
|
||||
private List<String> smtpServers;
|
||||
|
||||
@DeprecatedConfigurationProperty(reason = "not needed anymore", replacement = "none")
|
||||
public String getDefaultSubject() {
|
||||
return defaultSubject;
|
||||
}
|
||||
|
||||
public Boolean getEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(Boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public void setDefaultSubject(String defaultSubject) {
|
||||
this.defaultSubject = defaultSubject;
|
||||
}
|
||||
|
||||
public Duration getPauseBetweenMails() {
|
||||
return pauseBetweenMails;
|
||||
}
|
||||
|
||||
public void setPauseBetweenMails(Duration pauseBetweenMails) {
|
||||
this.pauseBetweenMails = pauseBetweenMails;
|
||||
}
|
||||
|
||||
public DataSize getMaxAttachmentSize() {
|
||||
return maxAttachmentSize;
|
||||
}
|
||||
|
||||
public void setMaxAttachmentSize(DataSize maxAttachmentSize) {
|
||||
this.maxAttachmentSize = maxAttachmentSize;
|
||||
}
|
||||
|
||||
public List<String> getSmtpServers() {
|
||||
return smtpServers;
|
||||
}
|
||||
|
||||
public void setSmtpServers(List<String> smtpServers) {
|
||||
this.smtpServers = smtpServers;
|
||||
}
|
||||
|
||||
public Weight getMaxAttachmentWeight() {
|
||||
return maxAttachmentWeight;
|
||||
}
|
||||
|
||||
public void setMaxAttachmentWeight(Weight maxAttachmentWeight) {
|
||||
this.maxAttachmentWeight = maxAttachmentWeight;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.reflectoring.configuration.mail;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Weight {
|
||||
|
||||
private static final Pattern PATTERN = Pattern.compile("^([0-9]+)(g|kg|t)$");
|
||||
|
||||
private long grams;
|
||||
|
||||
private Weight(long grams){
|
||||
this.grams = grams;
|
||||
}
|
||||
|
||||
public static Weight fromString(String string) {
|
||||
Matcher matcher = PATTERN.matcher(string);
|
||||
if (matcher.matches()) {
|
||||
Long amount = Long.valueOf(matcher.group(1));
|
||||
String unit = matcher.group(2);
|
||||
|
||||
switch (unit) {
|
||||
case "g":
|
||||
return new Weight(amount);
|
||||
case "kg":
|
||||
return new Weight(amount * 1_000);
|
||||
case "t":
|
||||
return new Weight(amount * 1_000 * 1_000);
|
||||
default:
|
||||
throw new IllegalArgumentException(String.format("invalid weight unit %s", unit));
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException(String.format("invalid weight string %s", string));
|
||||
}
|
||||
|
||||
public long getGrams() {
|
||||
return grams;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package io.reflectoring.configuration.mail;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
class WeightConverter implements Converter<String, Weight> {
|
||||
|
||||
@Override
|
||||
public Weight convert(String source) {
|
||||
return Weight.fromString(source);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.reflectoring.configuration.unknownandinvalidfield;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(
|
||||
prefix = "myapp.unknown-and-invalid-field-module",
|
||||
ignoreUnknownFields = false,
|
||||
ignoreInvalidFields = true)
|
||||
class UnkownAndInvalidFieldProperties {
|
||||
|
||||
private Boolean enabled;
|
||||
|
||||
public Boolean getEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(Boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.reflectoring.configuration.unknownfield;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
import io.reflectoring.configuration.mail.Weight;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "myapp.unknownfield", ignoreUnknownFields = false)
|
||||
class UnkownFieldProperties {
|
||||
|
||||
private Boolean enabled = Boolean.TRUE;
|
||||
|
||||
public Boolean getEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(Boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
myapp.mail.enabled=true
|
||||
myapp.mail.defaultSubject=hello
|
||||
myapp.mail.pauseBetweenMails=5s
|
||||
myapp.mail.maxAttachmentSize=1MB
|
||||
myapp.mail.smtpServers[0]=server1
|
||||
myapp.mail.smtpServers[1]=server2
|
||||
myapp.mail.maxAttachmentWeight=5kg
|
||||
myapp.mail.foo=foo
|
||||
@@ -0,0 +1,16 @@
|
||||
package io.reflectoring.configuration;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
class ConfigurationApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.reflectoring.configuration.mail;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(properties = {
|
||||
"myapp.mail.enabled=asd",
|
||||
"myapp.mail.defaultSubject=hello",
|
||||
"myapp.mail.pauseBetweenMails=5s",
|
||||
"myapp.mail.maxAttachmentSize=1MB",
|
||||
"myapp.mail.smtpServers[0]=server1",
|
||||
"myapp.mail.smtpServers[1]=server2",
|
||||
"myapp.mail.maxAttachmentWeight=5kg"
|
||||
})
|
||||
class MailModuleTestWithAllProperties {
|
||||
|
||||
@Autowired(required = false)
|
||||
private MailModuleProperties mailModuleProperties;
|
||||
|
||||
@Test
|
||||
void propertiesAreLoaded() {
|
||||
assertThat(mailModuleProperties).isNotNull();
|
||||
assertThat(mailModuleProperties.getDefaultSubject()).isEqualTo("hello");
|
||||
assertThat(mailModuleProperties.getEnabled()).isTrue();
|
||||
assertThat(mailModuleProperties.getPauseBetweenMails()).isEqualByComparingTo(Duration.ofSeconds(5));
|
||||
assertThat(mailModuleProperties.getMaxAttachmentSize()).isEqualByComparingTo(DataSize.ofMegabytes(1));
|
||||
assertThat(mailModuleProperties.getSmtpServers()).hasSize(2);
|
||||
assertThat(mailModuleProperties.getMaxAttachmentWeight().getGrams()).isEqualTo(5000L);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package io.reflectoring.configuration.mail;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(properties = {
|
||||
"myapp.mail.defaultSubject="
|
||||
})
|
||||
@Disabled("this test will fail due to invalid properties")
|
||||
class MailModuleTestWithInvalidProperties {
|
||||
|
||||
@Autowired(required = false)
|
||||
private MailModuleProperties mailModuleProperties;
|
||||
|
||||
@Test
|
||||
void propertiesAreLoaded() {
|
||||
assertThat(mailModuleProperties).isNotNull();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.reflectoring.configuration.mail;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
class MailModuleTestWithUnknownField {
|
||||
|
||||
@Autowired(required = false)
|
||||
private MailModuleProperties mailModuleProperties;
|
||||
|
||||
@Test
|
||||
void propertiesAreLoaded() {
|
||||
assertThat(mailModuleProperties).isNotNull();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.reflectoring.configuration.unknownandinvalidfield;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
|
||||
@SpringBootConfiguration
|
||||
@EnableConfigurationProperties(UnkownAndInvalidFieldProperties.class)
|
||||
class UnknownAndInvalidFieldModule {
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package io.reflectoring.configuration.unknownandinvalidfield;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(properties = {
|
||||
"myapp.unknown-and-invalid-field-module.unknown-property=foo"
|
||||
})
|
||||
class UnkownFieldPropertiesTest {
|
||||
|
||||
@Autowired(required = false)
|
||||
private UnkownAndInvalidFieldProperties properties;
|
||||
|
||||
@Test
|
||||
@Disabled("disabled due to unexpected behavior")
|
||||
void loadsApplicationContext() {
|
||||
// This test passes.
|
||||
// However, I would expect this test to fail application context startup due to the unknown property.
|
||||
// If we set ignoreInvalidFields to true, it works as expected.
|
||||
assertThat(properties).isNotNull();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package io.reflectoring.configuration.unknownfield;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@SpringBootConfiguration
|
||||
@EnableConfigurationProperties(UnkownFieldProperties.class)
|
||||
class UnknownFieldModule {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package io.reflectoring.configuration.unknownfield;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(properties = {
|
||||
"myapp.unknownfield.unknown-property=foo"
|
||||
})
|
||||
@Disabled("this test is expected to fail due an unknown property")
|
||||
class UnkownFieldPropertiesTest {
|
||||
|
||||
@Autowired(required = false)
|
||||
private UnkownFieldProperties properties;
|
||||
|
||||
@Test
|
||||
void propertiesAreLoaded() {
|
||||
assertThat(properties).isNotNull();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user