property conversion
This commit is contained in:
parent
29912c960e
commit
3b8bb69b10
|
@ -5,7 +5,7 @@ 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, PropertyConversion.class, EmployeeConverter.class })
|
||||
public class ConfigPropertiesDemoApplication {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String name;
|
||||
private double salary;
|
||||
|
||||
public Employee(String name, double salary) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationPropertiesBinding
|
||||
public class EmployeeConverter implements Converter<String, Employee> {
|
||||
|
||||
@Override
|
||||
public Employee convert(String from) {
|
||||
String[] data = from.split(",");
|
||||
return new Employee(data[0], Double.parseDouble(data[1]));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
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.context.annotation.PropertySource;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import org.springframework.util.unit.DataUnit;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:conversion.properties")
|
||||
@ConfigurationProperties(prefix = "conversion")
|
||||
public class PropertyConversion {
|
||||
private Duration timeInDefaultUnit;
|
||||
|
||||
private Duration timeInNano;
|
||||
|
||||
@DurationUnit(ChronoUnit.DAYS)
|
||||
private Duration timeInDays;
|
||||
|
||||
private DataSize sizeInDefaultUnit;
|
||||
|
||||
private DataSize sizeInGB;
|
||||
|
||||
@DataSizeUnit(DataUnit.TERABYTES)
|
||||
private DataSize sizeInTB;
|
||||
|
||||
private Employee employee;
|
||||
|
||||
// Getters and setters
|
||||
|
||||
public Duration getTimeInDefaultUnit() {
|
||||
return timeInDefaultUnit;
|
||||
}
|
||||
|
||||
public void setTimeInDefaultUnit(Duration timeInDefaultUnit) {
|
||||
this.timeInDefaultUnit = timeInDefaultUnit;
|
||||
}
|
||||
|
||||
public Duration getTimeInNano() {
|
||||
return timeInNano;
|
||||
}
|
||||
|
||||
public void setTimeInNano(Duration timeInNano) {
|
||||
this.timeInNano = timeInNano;
|
||||
}
|
||||
|
||||
public Duration getTimeInDays() {
|
||||
return timeInDays;
|
||||
}
|
||||
|
||||
public void setTimeInDays(Duration timeInDays) {
|
||||
this.timeInDays = timeInDays;
|
||||
}
|
||||
|
||||
public DataSize getSizeInDefaultUnit() {
|
||||
return sizeInDefaultUnit;
|
||||
}
|
||||
|
||||
public void setSizeInDefaultUnit(DataSize sizeInDefaultUnit) {
|
||||
this.sizeInDefaultUnit = sizeInDefaultUnit;
|
||||
}
|
||||
|
||||
public DataSize getSizeInGB() {
|
||||
return sizeInGB;
|
||||
}
|
||||
|
||||
public void setSizeInGB(DataSize sizeInGB) {
|
||||
this.sizeInGB = sizeInGB;
|
||||
}
|
||||
|
||||
public DataSize getSizeInTB() {
|
||||
return sizeInTB;
|
||||
}
|
||||
|
||||
public void setSizeInTB(DataSize sizeInTB) {
|
||||
this.sizeInTB = sizeInTB;
|
||||
}
|
||||
|
||||
public Employee getEmployee() {
|
||||
return employee;
|
||||
}
|
||||
|
||||
public void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ConfigPropertiesDemoApplication.class)
|
||||
@TestPropertySource("classpath:conversion.properties")
|
||||
public class PropertiesConversionIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private PropertyConversion properties;
|
||||
|
||||
@Test
|
||||
public void whenUseTimeUnitPropertyConversion_thenSuccess() throws Exception {
|
||||
assertEquals(Duration.ofMillis(10), properties.getTimeInDefaultUnit());
|
||||
assertEquals(Duration.ofNanos(9), properties.getTimeInNano());
|
||||
assertEquals(Duration.ofDays(2), properties.getTimeInDays());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseDataSizePropertyConversion_thenSuccess() throws Exception {
|
||||
assertEquals(DataSize.ofBytes(300), properties.getSizeInDefaultUnit());
|
||||
assertEquals(DataSize.ofGigabytes(2), properties.getSizeInGB());
|
||||
assertEquals(DataSize.ofTerabytes(4), properties.getSizeInTB());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseCustomPropertyConverter_thenSuccess() throws Exception {
|
||||
assertEquals("john", properties.getEmployee().getName());
|
||||
assertEquals(2000.0, properties.getEmployee().getSalary());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
###### time unit
|
||||
conversion.timeInDefaultUnit=10
|
||||
conversion.timeInNano=9ns
|
||||
conversion.timeInDays=2
|
||||
|
||||
###### data size
|
||||
conversion.sizeInDefaultUnit=300
|
||||
conversion.sizeInGB=2GB
|
||||
conversion.sizeInTB=4
|
||||
|
||||
conversion.employee=john,2000
|
Loading…
Reference in New Issue