引言
当我们通过@configurationproperties注解实现配置 bean的时候,如果默认的配置属性转换无法满足我们的需求的时候,我们可以根据自己的需求通过以下扩展方式对配置属性进行转换
propertyeditorsupport实现
下面的例子是把属性中定义的字符串转换成movie,并且把name的值大写
继承propertyeditorsupport并且实现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]类,转换数据={}", 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() }); // 第二 种 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]接口,转换数据={}", from); string[] data = from.split(","); return person.builder().name(data[0]).age(integer.parseint(data[1])).build(); } }
总结
@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 { //默认首先寻找bean名称叫conversionservice的conversionservice的bean类 return this.applicationcontext.getbean( configurableapplicationcontext.conversion_service_bean_name, conversionservice.class); } catch (nosuchbeandefinitionexception ex) { //找不到就默认生成applicationconversionservice类 return this.applicationcontext.getautowirecapablebeanfactory() .createbean(factory.class).create(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
荐 深入理解Java中的BigInteger和 BigDecimal,再也不怕面试了
tomact正常启动,但是在日志文件报错java.lang.NoClassDefFoundError: java/util/logging/Logger
servlet整合quartz:servlet中使用quartz,服务器启动时加载任务
荐 Java——集合中的Map接口通过HashMap类实现一些常用的方法
SpringBoot整合mybatis访问时报错Invalid bound statement (not found)
网友评论