淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類
一、萬能的字符串
當然,任何時候都可以使用字符串作為屬性的值,從配置文件里讀取出來,如下:
配置文件內(nèi)容為:
pkslow.admin=larry|18|admin@pkslow.com
通過|
分割,分別是名字、年齡和郵箱。
對應屬性為:
@Value("${pkslow.admin}") private String admin;
使用字符串,總是可以獲取,并且不會報錯。我們可以在使用屬性的時候,再轉(zhuǎn)換成其它Bean
。
但這樣做有一些問題:
- 無法做配置檢驗,不管是否配置錯誤,
String
類型的屬性都是可以讀取的; - 任何地方使用,都需要做顯式轉(zhuǎn)換。
二、自定義轉(zhuǎn)換類
使用自定義轉(zhuǎn)換類是更方便和安全的做法。我們來看看怎么實現(xiàn)。
先定義一個Java Bean
,用以表示實際的配置內(nèi)容:
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 }
接著肯定需要一個轉(zhuǎn)換類,需要實現(xiàn)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]); } }
這個轉(zhuǎn)換類就是轉(zhuǎn)換邏輯,如果把字符串轉(zhuǎn)換成對應的類。
完成以上兩步,關(guān)鍵是如果告訴Spring
我具備了這個轉(zhuǎn)換能力,并幫我轉(zhuǎn)換。需要把轉(zhuǎn)換類綁定一下:
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"}
說明成功讀取了配置,并轉(zhuǎn)換成我們想要的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"
可以做配置檢查。
三、總結(jié)
自定義轉(zhuǎn)換類還是非常有用的。
代碼請查看:https://github.com/LarryDpk/pkslow-samples
以上就是淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類的詳細內(nèi)容,更多關(guān)于Spring自定義轉(zhuǎn)換類的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis-Plus實現(xiàn)2種分頁方法(QueryWrapper查詢分頁和SQL查詢分頁)
本文主要介紹了MyBatis-Plus實現(xiàn)2種分頁方法,主要包括QueryWrapper查詢分頁和SQL查詢分頁,具有一定的參考價值,感興趣的可以了解一下2021-08-08spring boot activiti工作流的搭建與簡單使用
這篇文章主要給大家介紹了關(guān)于spring boot activiti工作流的搭建與簡單使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08MybatisPlus多數(shù)據(jù)源及事務解決思路
這篇文章主要介紹了MybatisPlus多數(shù)據(jù)源及事務解決思路,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01