java如何实现参数绑定

配置文件读取

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
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;

/**
* @author cuishiying
*/
@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);
});
}

}

配置binder

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
package cn.idea360.assistant;

import cn.idea360.assistant.properties.CommonDataSourceProperties;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.GenericConversionService;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;

/**
* @author cuishiying
*/
@Slf4j
public class BaseTest {

protected CommonDataSourceProperties sourceProperties;

protected CommonDataSourceProperties targetProperties;

@SneakyThrows
private Properties loadProperties() {
InputStream inputStream = ConfigLoadTest.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(inputStream);
return properties;
}

@SneakyThrows
private <T> T binder(Properties properties, Class<T> beanClass, String prefix) {
T bean = beanClass.getDeclaredConstructor().newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
String propertyName = propertyDescriptor.getName();
String propertyValue = properties.getProperty(prefix + "." + propertyName);
if (propertyValue != null) {
Method setter = propertyDescriptor.getWriteMethod();
Class<?> propertyType = propertyDescriptor.getPropertyType();
Object convertValue = convertValue(propertyValue, propertyType);
setter.invoke(bean, convertValue);
}
}
return bean;
}

private Object convertValue(String propertyValue, Class<?> propertyType) {
ConversionService conversionService = new GenericConversionService();
return conversionService.convert(propertyValue, propertyType);
}

@Before
public void init() {
Properties properties = loadProperties();
this.sourceProperties = binder(properties, CommonDataSourceProperties.class, "assistant.source");
this.targetProperties = binder(properties, CommonDataSourceProperties.class, "assistant.target");
}

@Test
public void log_init() {
log.info("base test init success");
}

}