淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類(lèi)
當(dāng)然,任何時(shí)候都可以使用字符串作為屬性的值,從配置文件里讀取出來(lái),如下:
配置文件內(nèi)容為:
pkslow.admin=larry|18|admin@pkslow.com
通過(guò)|分割,分別是名字、年齡和郵箱。
對(duì)應(yīng)屬性為:
@Value('${pkslow.admin}')private String admin;
使用字符串,總是可以獲取,并且不會(huì)報(bào)錯(cuò)。我們可以在使用屬性的時(shí)候,再轉(zhuǎn)換成其它Bean。
但這樣做有一些問(wèn)題:
無(wú)法做配置檢驗(yàn),不管是否配置錯(cuò)誤,String類(lèi)型的屬性都是可以讀取的; 任何地方使用,都需要做顯式轉(zhuǎn)換。二、自定義轉(zhuǎn)換類(lèi)使用自定義轉(zhuǎn)換類(lèi)是更方便和安全的做法。我們來(lái)看看怎么實(shí)現(xiàn)。
先定義一個(gè)Java Bean,用以表示實(shí)際的配置內(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}
接著肯定需要一個(gè)轉(zhuǎn)換類(lèi),需要實(shí)現(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]); }}
這個(gè)轉(zhuǎn)換類(lèi)就是轉(zhuǎn)換邏輯,如果把字符串轉(zhuǎn)換成對(duì)應(yīng)的類(lèi)。
完成以上兩步,關(guān)鍵是如果告訴Spring我具備了這個(gè)轉(zhuǎn)換能力,并幫我轉(zhuǎn)換。需要把轉(zhuǎn)換類(lèi)綁定一下:
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;@Configurationpublic class AdminConversionServiceConfig { @Bean public ConversionServiceFactoryBean conversionService() {ConversionServiceFactoryBean factoryBean = new ConversionServiceFactoryBean();factoryBean.setConverters(Collections.singleton(new AdminConverter()));return factoryBean; }}
有了以上功能,使用就非常簡(jiǎn)單了。配置不變,使用如下:
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;@RestControllerpublic class PkslowController { @Value('${pkslow.admin}') private Admin adminBean; @GetMapping('/getAdminBean') public Admin getAdminBean() {return adminBean; }}
屬性的類(lèi)型為Admin,是一個(gè)自定義的類(lèi)。啟動(dòng)訪問(wèn)后獲取如下:
$ curl localhost:8081/getAdminBean
{'name':'larry','age':18,'email':'admin@pkslow.com'}
說(shuō)明成功讀取了配置,并轉(zhuǎn)換成我們想要的domain Object。
嘗試把配置改為:pkslow.admin=larry|18a|admin@pkslow.com,則啟動(dòng)時(shí)會(huì)報(bào)錯(cuò):
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)換類(lèi)還是非常有用的。
代碼請(qǐng)查看:https://github.com/LarryDpk/pkslow-samples

以上就是淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類(lèi)的詳細(xì)內(nèi)容,更多關(guān)于Spring自定義轉(zhuǎn)換類(lèi)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python 合并拼接字符串的方法2. Linux刪除系統(tǒng)自帶版本Python過(guò)程詳解3. Python3 json模塊之編碼解碼方法講解4. Python 制作查詢商品歷史價(jià)格的小工具5. python 使用事件對(duì)象asyncio.Event來(lái)同步協(xié)程的操作6. ASP基礎(chǔ)知識(shí)VBScript基本元素講解7. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條8. Python 利用Entrez庫(kù)篩選下載PubMed文獻(xiàn)摘要的示例9. Python sublime安裝及配置過(guò)程詳解10. Python插件機(jī)制實(shí)現(xiàn)詳解

網(wǎng)公網(wǎng)安備