引言
當(dāng)我們通過@ConfigurationProperties注解實(shí)現(xiàn)配置 bean的時候,如果默認(rèn)的配置屬性轉(zhuǎn)換無法滿足我們的需求的時候,我們可以根據(jù)自己的需求通過以下擴(kuò)展方式對配置屬性進(jìn)行轉(zhuǎn)換
PropertyEditorSupport實(shí)現(xiàn)下面的例子是把屬性中定義的字符串轉(zhuǎn)換成Movie,并且把name的值大寫
繼承PropertyEditorSupport并且實(shí)現(xiàn)PropertyEditorRegistrar接口
package com.paderlol.spring.practice.properties.editor; import com.paderlol.spring.practice.properties.pojo.Movie; import java.beans.PropertyEditorSupport; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; /** * @author pader PropertyEditor 在不同的包下面 */ @Slf4j public class CustomMovieEditor extends PropertyEditorSupport implements PropertyEditorRegistrar { @Override public String getAsText() { Movie movie = (Movie) getValue(); return movie == null ? "" : movie.getName(); } @Override public void setAsText(String text) throws IllegalArgumentException { log.info("繼承[PropertyEditorSupport]類,轉(zhuǎn)換數(shù)據(jù)={}", text); String[] data = text.split("-"); Movie movie = Movie.builder().name(data[0] .toUpperCase()).seat(Integer.parseInt(data[1])) .build(); setValue(movie); } @Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Movie.class,this); } }
注冊自定義的PropertyEditor
@Bean public CustomEditorConfigurer customEditorConfigurer() { CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer(); // 有兩種注冊方式 這是第一種 customEditorConfigurer.setPropertyEditorRegistrars( new PropertyEditorRegistrar[]{ new CustomMovieEditor() }); // 第二 種 MapConverter接口+@ConfigurationPropertiesBinding注解,Class extends PropertyEditor>> maps = new HashMap<>(); maps.put(Movie.class,CustomMovieEditor.class); return customEditorConfigurer; }
//注意 @Component @ConfigurationPropertiesBinding public class StringToPersonConverter implements Converter總結(jié){ @Override public Person convert(String from) { log.info("使用[Converter]接口,轉(zhuǎn)換數(shù)據(jù)={}", from); String[] data = from.split(","); return Person.builder().name(data[0]).age(Integer.parseInt(data[1])).build(); } }
以上兩種實(shí)現(xiàn)方式結(jié)果,但是Converter接口相比PropertyEditor接口更加靈活一些,PropertyEditor接口僅限于String轉(zhuǎn)換,Converter可以自定義別的,并且PropertyEditor接口通常用于Controller中的接收參數(shù)的轉(zhuǎn)換。
@ConfigurationPropertiesBinding是限定符注解@Qualifier的派生類而已,參考org.springframework.boot.context.properties.ConversionServiceDeducer,以下是源代碼片段
@Autowired(required = false) @ConfigurationPropertiesBinding public void setConverters(List> converters) { this.converters = converters; } /** * A list of custom converters (in addition to the defaults) to use when * converting properties for binding. * @param converters the converters to set */ @Autowired(required = false) @ConfigurationPropertiesBinding public void setGenericConverters(List converters) { this.genericConverters = converters; }
Formatter接口是不能對屬性完成轉(zhuǎn)換的,因?yàn)?strong>ConversionServiceDeducer初始化的時候只獲取GenericConverter和Converter接口
官方文檔上還介紹了可以使用實(shí)現(xiàn)org.springframework.core.convert.ConversionService并且Bean名稱也必須叫conversionService,不過大部分情況不推薦自己通過這種方式去實(shí)現(xiàn)這個接口,因?yàn)樽约簩?shí)現(xiàn)的ConversionService會替代默認(rèn)的。具體參考ConversionServiceDeducer源碼:
public ConversionService getConversionService() { try { //默認(rèn)首先尋找Bean名稱叫conversionService的ConversionService的Bean類 return this.applicationContext.getBean( ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME, ConversionService.class); } catch (NoSuchBeanDefinitionException ex) { //找不到就默認(rèn)生成ApplicationConversionService類 return this.applicationContext.getAutowireCapableBeanFactory() .createBean(Factory.class).create(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/72074.html
摘要:的默認(rèn)配置文件位置為。比如,我們需要自定義模塊的服務(wù)端口號,可以在中添加來指定服務(wù)端口為,也可以通過來指定應(yīng)用名該名字在應(yīng)用中會被注冊為服務(wù)名。同時,配置內(nèi)容都對開發(fā)人員可見,本身這也是一種安全隱患。 在快速入門一節(jié)中,我們輕松的實(shí)現(xiàn)了一個簡單的RESTful API應(yīng)用,體驗(yàn)了一下Spring Boot給我們帶來的諸多優(yōu)點(diǎn),我們用非常少的代碼量就成功的實(shí)現(xiàn)了一個Web應(yīng)用,這是傳統(tǒng)的...
摘要:比如,在中,不能將屬性綁定到對象。引入了新的接口,能夠指出屬性取值的準(zhǔn)確位置。比如,屬性綁定的驗(yàn)證異?,F(xiàn)在會顯示類允許你使用多個。我們計劃在中繼續(xù)加強(qiáng)的功能,而第一個想要支持的功能是不可變屬性綁定。 Spring Boot2.0的屬性綁定 原文從Spring boot第一個版本以來,我們可以使用@ConfigurationProperties注解將屬性綁定到對象。也可以指定屬性的各種不...
摘要:引言的一個便捷功能是外部化配置,可以輕松訪問屬性文件中定義的屬性。本文將詳細(xì)介紹的使用。 引言 Spring Boot的一個便捷功能是外部化配置,可以輕松訪問屬性文件中定義的屬性。本文將詳細(xì)介紹@ConfigurationProperties的使用。 配置項(xiàng)目POM 在pom.xml中定義Spring-Boot 為parent org.springframework.boot...
摘要:可以使用外部化配置來方便在不同環(huán)境的運(yùn)行同樣的程序文件文件環(huán)境變量命令行參數(shù)內(nèi)置順序?qū)崿F(xiàn)了很多按以下順序進(jìn)行合理的相同屬性的覆蓋目錄下的全局設(shè)置屬性,如果激活測試用例上的注解測試用例上的注解。 簡介 在應(yīng)用中管理配置并不是一個容易的任務(wù),尤其是在應(yīng)用需要部署到多個環(huán)境中時。通常會需要為每個環(huán)境提供一個對應(yīng)的屬性文件,用來配置各自的數(shù)據(jù)庫連接信息、服務(wù)器信息和第三方服務(wù)賬號等。通常的應(yīng)用...
摘要:完成狀態(tài)編寫中已完成維護(hù)中原文是一個使用編寫的開源支持網(wǎng)絡(luò)基于內(nèi)存可選持久性的鍵值對存儲數(shù)據(jù)庫維基百科是目前業(yè)界使用廣泛的基于內(nèi)存的數(shù)據(jù)庫。 完成狀態(tài) [ ] 編寫中 [ ] 已完成 [x] 維護(hù)中 原文 Redis Redis是一個使用ANSI C編寫的開源、支持網(wǎng)絡(luò)、基于內(nèi)存、可選持久性的鍵值對存儲數(shù)據(jù)庫 ------ 維基百科 Redis 是目前業(yè)界使用廣泛的基于內(nèi)存的...
閱讀 1600·2023-04-25 17:41
閱讀 3110·2021-11-22 15:08
閱讀 913·2021-09-29 09:35
閱讀 1678·2021-09-27 13:35
閱讀 3403·2021-08-31 09:44
閱讀 2774·2019-08-30 13:20
閱讀 2011·2019-08-30 13:00
閱讀 2624·2019-08-26 12:12