淺談讓@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;@Configurationpublic 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;@RestControllerpublic 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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python+unittest+requests 接口自動化測試框架搭建教程2. Python的文本常量與字符串模板之string庫3. 利用CSS制作3D動畫4. 存儲于xml中需要的HTML轉(zhuǎn)義代碼5. 完美解決vue 中多個echarts圖表自適應的問題6. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進)7. 一款功能強大的markdown編輯器tui.editor使用示例詳解8. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程9. Java GZip 基于內(nèi)存實現(xiàn)壓縮和解壓的方法10. SpringBoot+TestNG單元測試的實現(xiàn)