使用spring中的Binder绑定参数


在使用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;
        }
		// 进行转换操作,参数分别是:值、值的类、需要转换的目标类。这里demo就固定返回了当前时间
        @Override
        public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            return LocalDateTime.now();
        }
    }

定义ConversionService的实现类:

public class MyGenericConversionService extends GenericConversionService{
		// 根据目标类和结果类判断是否能进行处理,返回null则代表无法处理
        @Nullable
        protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
            return new MyGenericConverter();
        }
        // 执行转换,这里获取了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}

…待更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值