栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用spring中的Binder绑定参数

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用spring中的Binder绑定参数

使用spring中的Binder绑定参数
  • 1 简单用法
  • 2 更多功能
    • 2.1 字段的序列化
    • 2.2 参数验证

在使用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 bindable = Bindable.ofInstance(user);
        // 这里构建一个虚拟的配置文件
        Map properties = new HashMap<>();
        properties.put("user.name", "三侃");
        properties.put("user.age", "18");
        List 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 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 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 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 bindable = Bindable.ofInstance(user);
        Map properties = new HashMap<>();
        properties.put("user.name", "三侃");
        properties.put("user.birthday", "202");
        BindHandler handler = new IgnoreTopLevelConverterNotFoundBindHandler();
        List 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 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 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}

…待更新

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1017625.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号