久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類

瀏覽:48日期:2023-07-07 14:43:49
目錄一、萬能的字符串二、自定義轉(zhuǎn)換類三、總結(jié)一、萬能的字符串

當(dāng)然,任何時候都可以使用字符串作為屬性的值,從配置文件里讀取出來,如下:

配置文件內(nèi)容為:

pkslow.admin=larry|18|admin@pkslow.com

通過|分割,分別是名字、年齡和郵箱。

對應(yīng)屬性為:

@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)換成對應(yīng)的類。

完成以上兩步,關(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)換類

以上就是淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類的詳細(xì)內(nèi)容,更多關(guān)于Spring自定義轉(zhuǎn)換類的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 应城市| 九江县| 德钦县| 清远市| 红安县| 澄迈县| 嘉禾县| 门源| 绩溪县| 武鸣县| 长泰县| 富宁县| 宁明县| 红桥区| 通榆县| 洮南市| 会同县| 潞西市| 大石桥市| 横山县| 锡林郭勒盟| 浮梁县| 将乐县| 济源市| 登封市| 汝城县| 浦江县| 达拉特旗| 孟州市| 晋城| 晋中市| 奉节县| 夏津县| 繁峙县| 齐河县| 石河子市| 邵阳市| 樟树市| 玉屏| 石城县| 大理市|