浅谈让@Value更方便的Spring自定义转换类
作者:南瓜慢说 时间:2023-10-06 14:01:02
一、万能的字符串
当然,任何时候都可以使用字符串作为属性的值,从配置文件里读取出来,如下:
配置文件内容为:
pkslow.admin=larry|18|admin@pkslow.com
通过|
分割,分别是名字、年龄和邮箱。
对应属性为:
@Value("${pkslow.admin}")
private String admin;
使用字符串,总是可以获取,并且不会报错。我们可以在使用属性的时候,再转换成其它Bean
。
但这样做有一些问题:
无法做配置检验,不管是否配置错误,
String
类型的属性都是可以读取的;任何地方使用,都需要做显式转换。
二、自定义转换类
使用自定义转换类是更方便和安全的做法。我们来看看怎么实现。
先定义一个Java Bean
,用以表示实际的配置内容:
package com.pkslow.cloud.rest.model;
public class Admin {
private String name;
private Integer age;
private String email;
public Admin(String name, Integer age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
//getter and setter
}
接着肯定需要一个转换类,需要实现Converter
接口:
package com.pkslow.cloud.rest.model;
import org.springframework.core.convert.converter.Converter;
public class AdminConverter implements Converter<String, Admin> {
@Override
public Admin convert(String s) {
String[] strings = s.split("\\|");
return new Admin(strings[0], Integer.parseInt(strings[1]), strings[2]);
}
}
这个转换类就是转换逻辑,如果把字符串转换成对应的类。
完成以上两步,关键是如果告诉Spring
我具备了这个转换能力,并帮我转换。需要把转换类绑定一下:
package com.pkslow.cloud.rest.config;
import com.pkslow.cloud.rest.model.AdminConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ConversionServiceFactoryBean;
import java.util.Collections;
@Configuration
public class AdminConversionServiceConfig {
@Bean
public ConversionServiceFactoryBean conversionService() {
ConversionServiceFactoryBean factoryBean = new ConversionServiceFactoryBean();
factoryBean.setConverters(Collections.singleton(new AdminConverter()));
return factoryBean;
}
}
有了以上功能,使用就非常简单了。配置不变,使用如下:
package com.pkslow.cloud.rest;
import com.pkslow.cloud.rest.model.Admin;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PkslowController {
@Value("${pkslow.admin}")
private Admin adminBean;
@GetMapping("/getAdminBean")
public Admin getAdminBean() {
return adminBean;
}
}
属性的类型为Admin
,是一个自定义的类。启动访问后获取如下:
$ curl localhost:8081/getAdminBean
{"name":"larry","age":18,"email":"admin@pkslow.com"}
说明成功读取了配置,并转换成我们想要的domain Object
。
尝试把配置改为:pkslow.admin=larry|18a|admin@pkslow.com
,则启动时会报错:
Caused by: org.springframework.core.convert.ConversionFailedException:
Failed to convert from type [java.lang.String] to type [@org.springframework.beans.factory.annotation.Value com.pkslow.cloud.rest.model.Admin]
for value 'larry|18a|admin@pkslow.com';
nested exception is java.lang.NumberFormatException: For input string: "18a"
可以做配置检查。
三、总结
自定义转换类还是非常有用的。
代码请查看:https://github.com/LarryDpk/pkslow-samples
来源:https://www.cnblogs.com/larrydpk/p/14916921.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JFinal实现伪静态的方法
![](https://img.aspxhome.com/file/2023/0/57630_0s.jpg)
Android 帧动画的实例详解
C#实现上位机与欧姆龙PLC通讯(FINS)
C#操作INI文件的辅助类IniHelper
BeanUtils.copyProperties在拷贝属性时忽略空值的操作
java中的各种修饰符作用及范围
jenkins安装及其配置笔记
![](https://img.aspxhome.com/file/2023/8/64048_0s.png)
Java基础之位运算知识总结
![](https://img.aspxhome.com/file/2023/3/66043_0s.png)
Springboot实现图片上传功能的示例代码
![](https://img.aspxhome.com/file/2023/2/62722_0s.png)
Hibernate中load方法与get方法的区别
C#实现插入排序
![](https://img.aspxhome.com/file/2023/7/102317_0s.png)
Java基础之static关键字的使用讲解
![](https://img.aspxhome.com/file/2023/9/66039_0s.jpg)
C#读取XML的三种实现方式
![](https://img.aspxhome.com/file/2023/8/102178_0s.png)
Flink开发IDEA环境搭建与测试的方法
![](https://img.aspxhome.com/file/2023/0/84270_0s.jpg)
Android SQLite详解及示例代码
![](https://img.aspxhome.com/file/2023/3/84753_0s.png)
C#虚函数用法实例分析
Android实现动态定值范围效果的控件
![](https://img.aspxhome.com/file/2023/3/94953_0s.gif)