引言
從策劃到設(shè)計(jì)制作,每一步都追求做到細(xì)膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站策劃、網(wǎng)頁設(shè)計(jì)、域名與空間、網(wǎng)站空間、網(wǎng)絡(luò)營(yíng)銷、VI設(shè)計(jì)、 網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進(jìn)步。
當(dāng)我們通過@ConfigurationProperties注解實(shí)現(xiàn)配置 bean的時(shí)候,如果默認(rèn)的配置屬性轉(zhuǎn)換無法滿足我們的需求的時(shí)候,我們可以根據(jù)自己的需求通過以下擴(kuò)展方式對(duì)配置屬性進(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); } }
注冊(cè)自定義的PropertyEditor
@Bean public CustomEditorConfigurer customEditorConfigurer() { CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer(); // 有兩種注冊(cè)方式 這是第一種 customEditorConfigurer.setPropertyEditorRegistrars( new PropertyEditorRegistrar[]{ new CustomMovieEditor() }); // 第二 種 Map,Class<? extends PropertyEditor>> maps = new HashMap<>(); maps.put(Movie.class,CustomMovieEditor.class); return customEditorConfigurer; }
Converter接口+@ConfigurationPropertiesBinding注解
//注意 @Component @ConfigurationPropertiesBinding public class StringToPersonConverter implements Converter{ @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> 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; }
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(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。