引言
當我們通過@configurationproperties注解實現(xiàn)配置 bean的時候,如果默認的配置屬性轉(zhuǎn)換無法滿足我們的需求的時候,我們可以根據(jù)自己的需求通過以下擴展方式對配置屬性進行轉(zhuǎn)換
propertyeditorsupport實現(xiàn)
下面的例子是把屬性中定義的字符串轉(zhuǎn)換成movie,并且把name的值大寫
繼承propertyeditorsupport并且實現(xiàn)propertyeditorregistrar接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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
1
2
3
4
5
6
7
8
9
10
11
12
|
@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注解
1
2
3
4
5
6
7
8
9
10
11
12
|
//注意 @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é)
- 以上兩種實現(xiàn)方式結(jié)果,但是converter接口相比propertyeditor接口更加靈活一些,propertyeditor接口僅限于string轉(zhuǎn)換,converter可以自定義別的,并且propertyeditor接口通常用于controller中的接收參數(shù)的轉(zhuǎn)換。
- @configurationpropertiesbinding是限定符注解@qualifier的派生類而已,參考org.springframework.boot.context.properties.conversionservicededucer,以下是源代碼片段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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; } |
- formatter接口是不能對屬性完成轉(zhuǎn)換的,因為conversionservicededucer初始化的時候只獲取genericconverter和converter接口
- 官方文檔上還介紹了可以使用實現(xiàn)org.springframework.core.convert.conversionservice并且bean名稱也必須叫conversionservice,不過大部分情況不推薦自己通過這種方式去實現(xiàn)這個接口,因為自己實現(xiàn)的conversionservice會替代默認的。具體參考conversionservicededucer源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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(); } } |
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000016941868