您好,登錄后才能下訂單哦!
引言
當(dāng)我們通過(guò)@ConfigurationProperties注解實(shí)現(xiàn)配置 bean的時(shí)候,如果默認(rèn)的配置屬性轉(zhuǎn)換無(wú)法滿(mǎn)足我們的需求的時(shí)候,我們可以根據(jù)自己的需求通過(guò)以下擴(kuò)展方式對(duì)配置屬性進(jìn)行轉(zhuǎn)換
PropertyEditorSupport實(shí)現(xiàn)
下面的例子是把屬性中定義的字符串轉(zhuǎn)換成Movie,并且把name的值大寫(xiě)
繼承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]類(lèi),轉(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); } }
注冊(cè)自定義的PropertyEditor
@Bean public CustomEditorConfigurer customEditorConfigurer() { CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer(); // 有兩種注冊(cè)方式 這是第一種 customEditorConfigurer.setPropertyEditorRegistrars( new PropertyEditorRegistrar[]{ new CustomMovieEditor() }); // 第二 種 Map<Class<?>,Class<? extends PropertyEditor>> maps = new HashMap<>(); maps.put(Movie.class,CustomMovieEditor.class); return customEditorConfigurer; }
Converter接口+@ConfigurationPropertiesBinding注解
//注意 @Component @ConfigurationPropertiesBinding public class StringToPersonConverter implements Converter<String, Person> { @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(); } }
總結(jié)
@Autowired(required = false) @ConfigurationPropertiesBinding public void setConverters(List<Converter<?, ?>> 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<GenericConverter> converters) { this.genericConverters = converters; }
public ConversionService getConversionService() { try { //默認(rèn)首先尋找Bean名稱(chēng)叫conversionService的ConversionService的Bean類(lèi) return this.applicationContext.getBean( ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME, ConversionService.class); } catch (NoSuchBeanDefinitionException ex) { //找不到就默認(rèn)生成ApplicationConversionService類(lèi) return this.applicationContext.getAutowireCapableBeanFactory() .createBean(Factory.class).create(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。