在使用spring boot时发现参数绑定非常好用,简单明了的绑定完一整个实体的属性。像是这样:
user.name=三侃
user.age=30
可以直接绑定到下面这个实体中
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private String name;
private Integer age;
private LocalDateTime birthday;
虽然用了很久已经,但是依然感觉很炫酷。 就想自己写的代码也用上这个功能,后来在翻看spring源码时发现一个类:Binder。研究了一下他的用法,发现可以自己实现这种炫酷的绑定变量方式了。
1 简单用法
首先最简单的用法:
public static void main(String[] args) {
User user = new User();
Bindable<User> bindable = Bindable.ofInstance(user);
Map properties = new HashMap<>();
properties.put("user.name", "三侃");
properties.put("user.age", "18");
List<ConfigurationPropertySource> propertySources = Collections
.singletonList(new MapConfigurationPropertySource(properties));
user = new Binder(propertySources).bindOrCreate("user", bindable);
System.out.println(user);
}
输出文本:
User{name='三侃', age=18}
2 更多功能
只有最基本的功能肯定是无法在正常开发中使用的,不过强大的spring提供了非常丰富的功能。
2.1 字段的序列化
public static void main(String[] args) {
User user = new User();
Bindable<User> bindable = Bindable.ofInstance(user);
Map properties = new HashMap<>();
properties.put("user.name", "三侃");
properties.put("user.age", "18");
properties.put("user.birthday", "2022-01-01 00:00:00");
DateTimeFormatters dateTimeFormatters = new DateTimeFormatters().dateTimeFormat("yyyy-MM-dd HH:mm:ss");
List<ConfigurationPropertySource> propertySources = Collections
.singletonList(new MapConfigurationPropertySource(properties));
user = new Binder(propertySources, null, new WebConversionService(dateTimeFormatters)).bindOrCreate("user", bindable);
System.out.println(user);
}
输出文本:
User{name='三侃', age=18, birthday=2022-01-01T00:00}
除了这种使用开源工具自带的还可以自己实现转换的逻辑。 主要包含俩种接口类 GenericConverter:实际进行判断和执行转换的类 ConversionService:提供类型转换完整服务,包括判断是否可以转换和实际进行转换的入口。可以理解为封装了GenericConverter的功能
定义GenericConverter实现类
public class MyGenericConverter implements GenericConverter{
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return null;
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return LocalDateTime.now();
}
}
定义ConversionService的实现类:
public class MyGenericConversionService extends GenericConversionService{
@Nullable
protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
return new MyGenericConverter();
}
@Override
@Nullable
public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
Assert.notNull(targetType, "Target type to convert to cannot be null");
return getConverter(sourceType, targetType).convert(source, sourceType, targetType);
}
}
最后使用自定义的转换
public static void main(String[] args) {
User user = new User();
Bindable<User> bindable = Bindable.ofInstance(user);
Map properties = new HashMap<>();
properties.put("user.name", "三侃");
properties.put("user.birthday", "202");
BindHandler handler = new IgnoreTopLevelConverterNotFoundBindHandler();
List<ConfigurationPropertySource> propertySources = Collections
.singletonList(new MapConfigurationPropertySource(properties));
user = new Binder(propertySources, null, new MyGenericConversionService())
.bindOrCreate("user", bindable, handler);
System.out.println(user);
}
因为我demo中返回的转换结果是当前时间,所以用户生日这个字段无论如何都只会是当前时间。
2.2 参数验证
public static void main(String[] args) {
User user = new User();
Bindable<User> bindable = Bindable.ofInstance(user);
Map properties = new HashMap<>();
properties.put("user.name", "三侃");
properties.put("user.age", "-1");
properties.put("user.birthday", "2022-01-01 00:00:00");
Validator validator = new Validator() {
@Override
public boolean supports(Class<?> clazz) {
return clazz == Integer.class;
}
@Override
public void validate(Object target, Errors errors) {
if((int)target<0){
System.out.println("年龄不能小于0");
}
}
};
ValidationBindHandler handler = new ValidationBindHandler(new IgnoreTopLevelConverterNotFoundBindHandler(), validator);
DateTimeFormatters dateTimeFormatters = new DateTimeFormatters().dateTimeFormat("yyyy-MM-dd HH:mm:ss");
List<ConfigurationPropertySource> propertySources = Collections
.singletonList(new MapConfigurationPropertySource(properties));
user = new Binder(propertySources, null, new WebConversionService(dateTimeFormatters))
.bindOrCreate("user", bindable, handler);
System.out.println(user);
}
输处文本:
年龄不能小于0
User{name='三侃', age=-1, birthday=2022-01-01T00:00}
…待更新
|