1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| package cn.idea360.assistant;
import cn.idea360.assistant.properties.CommonDataSourceProperties; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.util.Properties;
@Slf4j public class ConfigLoadTest {
@Test public void load_properties() throws IOException { ClassPathResource resource = new ClassPathResource("db.properties"); Properties properties = PropertiesLoaderUtils.loadProperties(resource); properties.forEach((k, v) -> { log.info("{}: {}", k, v); }); }
@Test public void load_properties1() throws IOException { InputStream inputStream = ConfigLoadTest.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream); properties.forEach((k, v) -> { log.info("{}: {}", k, v); }); }
@Test public void load_properties2() throws IOException { ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource("classpath:db.properties"); InputStream inputStream = resource.getInputStream(); Properties properties = new Properties(); properties.load(inputStream); properties.forEach((k, v) -> { log.info("{}: {}", k, v); }); }
@SneakyThrows @Test public void binder() { ClassPathResource resource = new ClassPathResource("db.properties"); Properties properties = PropertiesLoaderUtils.loadProperties(resource);
CommonDataSourceProperties bean = CommonDataSourceProperties.class.getDeclaredConstructor().newInstance(); BeanInfo beanInfo = Introspector.getBeanInfo(CommonDataSourceProperties.class); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { String propertyName = propertyDescriptor.getName(); String propertyValue = properties.getProperty("assistant.db.source." + propertyName); if (propertyValue != null) { Method setter = propertyDescriptor.getWriteMethod(); Class<?> propertyType = propertyDescriptor.getPropertyType(); Object convertValue = convertValue(propertyValue, propertyType); setter.invoke(bean, convertValue); } } System.out.println(bean); }
private Object convertValue(String propertyValue, Class<?> propertyType) { ConversionService conversionService = new DefaultConversionService(); return conversionService.convert(propertyValue, propertyType); }
}
|