This commit is contained in:
m.raheem 2020-01-23 17:26:30 +02:00
parent b7c1eea772
commit 0104ff9c82
18 changed files with 492 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package com.baeldung.boot.configurationproperties;
public class Connection {
private String authorization;
private int timeout;
public String getAuthorization() {
return authorization;
}
public void setAuthorization(String authorization) {
this.authorization = authorization;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.boot.configurationproperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConnectionFactory {
@Bean
@ConfigurationProperties(prefix = "testing")
public Connection getTestingConnection() {
return new Connection();
}
@Bean
@ConfigurationProperties(prefix = "live")
public Connection getLiveConnection() {
return new Connection();
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.boot.configurationproperties;
public class Credentials {
private String username;
private String password;
public Credentials() {System.out.println("### INIT2 ###");}
public Credentials(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.boot.configurationproperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
@Component
@ConfigurationPropertiesBinding
public class CustomCredentialsConverter implements Converter<String, Credentials> {
@Override
public Credentials convert(String source) {
String data[] = source.split(",");
return new Credentials(data[0], data[1]);
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.boot.configurationproperties;
import java.util.Map;
import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.validation.annotation.Validated;
@Configuration
@ConfigurationProperties(prefix = "validate")
@PropertySource("classpath:property-validation.properties")
@Validated
public class MailServer {
@NotNull
@NotEmpty
private Map<String, @NotBlank String> propertiesMap;
@Valid
private MailConfig mailConfig = new MailConfig();
public static class MailConfig {
@NotBlank
@Email
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public Map<String, String> getPropertiesMap() {
return propertiesMap;
}
public void setPropertiesMap(Map<String, String> propertiesMap) {
this.propertiesMap = propertiesMap;
}
public MailConfig getMailConfig() {
return mailConfig;
}
public void setMailConfig(MailConfig mailConfig) {
this.mailConfig = mailConfig;
}
}

View File

@ -0,0 +1,67 @@
package com.baeldung.boot.configurationproperties;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
@Configuration
@ConfigurationProperties(prefix = "server")
public class PropertyConversion {
private DataSize uploadSpeed;
@DataSizeUnit(DataUnit.GIGABYTES)
private DataSize downloadSpeed;
private Duration backupDay;
@DurationUnit(ChronoUnit.HOURS)
private Duration backupHour;
private Credentials credentials;
public Duration getBackupDay() {
return backupDay;
}
public void setBackupDay(Duration backupDay) {
this.backupDay = backupDay;
}
public Duration getBackupHour() {
return backupHour;
}
public void setBackupHour(Duration backupHour) {
this.backupHour = backupHour;
}
public DataSize getUploadSpeed() {
return uploadSpeed;
}
public void setUploadSpeed(DataSize uploadSpeed) {
this.uploadSpeed = uploadSpeed;
}
public DataSize getDownloadSpeed() {
return downloadSpeed;
}
public void setDownloadSpeed(DataSize downloadSpeed) {
this.downloadSpeed = downloadSpeed;
}
public Credentials getCredentials() {
return credentials;
}
public void setCredentials(Credentials credentials) {
this.credentials = credentials;
}
}

View File

@ -0,0 +1,70 @@
package com.baeldung.boot.configurationproperties;
import java.util.Map;
import java.util.Set;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "server")
public class ServerConfig {
private String name;
private Address address;
private Map<String, String> dirs;
private Set<String> imgIds;
public static class Address {
private String ip;
private int port;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Set<String> getImgIds() {
return imgIds;
}
public void setImgIds(Set<String> imgIds) {
this.imgIds = imgIds;
}
public Map<String, String> getDirs() {
return dirs;
}
public void setDirs(Map<String, String> dirs) {
this.dirs = dirs;
}
}

View File

@ -0,0 +1,4 @@
validate.propertiesMap.first=prop1
validate.propertiesMap.second=prop2
validate.mail_config.address=user1@test

View File

@ -0,0 +1,25 @@
package com.baeldung.boot.configurationproperties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
@SpringBootTest
@TestPropertySource("classpath:thirdparty-config-test.properties")
public class BindingPropertiesToThirdpartyPOJOUnitTest {
@Autowired
private ConnectionFactory connectionfactory;
@Test
void givenThirdPartyPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() {
assertEquals("foo", connectionfactory.getTestingConnection().getAuthorization());
assertEquals(50, connectionfactory.getTestingConnection().getTimeout());
assertEquals("bar", connectionfactory.getLiveConnection().getAuthorization());
assertEquals(100, connectionfactory.getLiveConnection().getTimeout());
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.boot.configurationproperties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
@SpringBootTest
@TestPropertySource("classpath:server-config-test.properties")
public class BindingPropertiesToUserDefinedPOJOUnitTest {
@Autowired
private ServerConfig serverConfig;
@Test
void givenUserDefinedPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() {
assertEquals("node1", serverConfig.getName());
assertEquals(Set.of("img1.jpg", "img2.jpg"),
serverConfig.getImgIds());
assertEquals("192.168.0.1", serverConfig.getAddress().getIp());
assertEquals(8099, serverConfig.getAddress().getPort());
assertEquals(Map.of("imgs", "/root/imgs", "html", "/root/html"),
serverConfig.getDirs());
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.boot.configurationproperties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class BindingYMLPropertiesUnitTest {
@Autowired
private ServerConfig serverConfig;
@Test
void whenBindingYMLConfigFile_thenAllFieldsAreSet() {
assertEquals("node2", serverConfig.getName());
assertEquals(Set.of("img1.png", "img2.png"),
serverConfig.getImgIds());
assertEquals("192.168.0.2", serverConfig.getAddress().getIp());
assertEquals(5000, serverConfig.getAddress().getPort());
assertEquals(Map.of("imgs", "/etc/imgs", "html", "/etc/html"),
serverConfig.getDirs());
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.boot.configurationproperties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import javax.validation.Validation;
import javax.validation.Validator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
@SpringBootTest
@TestPropertySource("classpath:property-validation-test.properties")
public class PropertyValidationUnitTest {
@Autowired
private MailServer mailServer;
private static Validator propertyValidator;
@BeforeAll
public static void setup() {
propertyValidator = Validation.buildDefaultValidatorFactory().getValidator();
}
@Test
void whenBindingPropertiesToValidatedBeans_thenConstrainsAreChecked() {
assertEquals(0, propertyValidator.validate(mailServer.getPropertiesMap()).size());
assertEquals(0, propertyValidator.validate(mailServer.getMailConfig()).size());
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.boot.configurationproperties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.util.unit.DataSize;
@SpringBootTest
@TestPropertySource("classpath:spring-conversion-test.properties")
public class SpringPropertiesConversionUnitTest {
@Autowired
private PropertyConversion propertyConversion;
@Test
void whenUsingSpringDefaultSizeConversion_thenDataSizeObjectIsSet() {
assertEquals(DataSize.ofMegabytes(500), propertyConversion.getUploadSpeed());
assertEquals(DataSize.ofGigabytes(10), propertyConversion.getDownloadSpeed());
}
@Test
void whenUsingSpringDefaultDurationConversion_thenDurationObjectIsSet() {
assertEquals(Duration.ofDays(1), propertyConversion.getBackupDay());
assertEquals(Duration.ofHours(8), propertyConversion.getBackupHour());
}
@Test
void whenRegisteringCustomCredentialsConverter_thenCredentialsAreParsed() {
assertEquals("user", propertyConversion.getCredentials().getUsername());
assertEquals("123", propertyConversion.getCredentials().getPassword());
}
}

View File

@ -0,0 +1,11 @@
server:
name: node2
address:
ip: 192.168.0.2
port: 5000
dirs:
imgs: /etc/imgs
html: /etc/html
imgIds:
- img1.png
- img2.png

View File

@ -0,0 +1,4 @@
validate.propertiesMap.first=prop1
validate.propertiesMap.second=
validate.mail_config.address=user1.test

View File

@ -0,0 +1,11 @@
server.NAME=node1
server.address.ip=192.168.0.1
server.address.port=8099
# directories
server.dirs.imgs=/root/imgs
server.dirs.html=/root/html
server.img_ids[0]=img1.jpg
server.img_ids[1]=img2.jpg

View File

@ -0,0 +1,10 @@
# bandwidth
server.upload_speed=500MB
server.download_speed=10
# backup date
server.backup_day=1d
server.backup_hour=8
# custom converter
server.credentials=user,123

View File

@ -0,0 +1,7 @@
# testing
testing.authorization=foo
testing.timeout=50
# live
live.authorization=bar
live.timeout=100