一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring Boot 2.0 配置屬性自定義轉(zhuǎn)換的方法

Spring Boot 2.0 配置屬性自定義轉(zhuǎn)換的方法

2021-06-10 15:28paderlol Java教程

這篇文章主要介紹了Spring Boot 2.0 配置屬性自定義轉(zhuǎn)換的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

引言

當我們通過@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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99视频观看 | 日韩欧美国产成人 | 久久热这里面只有精品 | 亚洲福利一区二区精品秒拍 | 视频一区在线免费观看 | 特黄特黄aaaa级毛片免费看 | 强插美女 | 亚洲精品免费在线 | 欧美日韩精品免费一区二区三区 | 国产一卡 | 国产中文在线视频 | 国产精品最新 | 第一次破女视频国产一级 | 亚洲AV无码一区二区三区乱子伦 | 亚洲男gay同性同志 亚洲免费在线看 | 久久这里只有精品无码3D | 91成人啪国产啪永久地址 | 91短视频在线免费观看 | 欧美伊香蕉久久综合类网站 | 贰佰麻豆剧果冻传媒一二三区 | 免费永久观看美女视频网站网址 | 狠狠色综合久久久久尤物 | 99热国产这里只有精品 | 我的妹妹最近有点怪在线观看 | 无限好资源免费观看 | 天堂资源wwww在线看 | 精品视频在线观看免费 | 精品一区二区三区免费视频 | 国产成人盗摄精品 | 日韩在线中文字幕 | 男男按摩1069gⅴ | 亚洲成人99 | caoporn国产 | 国产ab | 性吟网 | 国产在亚洲线视频观看 | 99精品免费观看 | 色综合网亚洲精品久久 | 国产精品高清一区二区三区 | 国产91 最新 在线 | 国产馆在线观看免费的 |