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

服務(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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: katsuniav在线播放 | 久久久久久久伊人电影 | 91九色视频无限观看免费 | 男人曰女人 | 精品久久久久久久久免费影院 | 短篇小说肉| 国产成人免费高清激情视频 | 国产卡一卡二卡3卡乱码免费 | 日韩欧美成末人一区二区三区 | 国产精品久久久久久福利 | 欧美乱理伦另类视频 | 亚洲精品国产一区二区在线 | 国产精品福利在线观看免费不卡 | 黄瓜视频黄 | 精品视频一区二区三区免费 | 精品一产品大全 | bl文全肉高h湿被灌尿 | yellow在线| 亚洲色图150p | 80日本xxxxxxxxx96| 万域之王动漫在线观看全集免费播放 | 免费观看一级一片 | 99在线观看视频免费 | 双性肉文高h | 天使萌痴汉在线中文字幕 | 色国产视频 | 精品综合 | 性做久久久久免费观看 | porno18老师hd| 亚洲高清无码在线 视频 | 婷婷综合缴情亚洲五月伊 | 欧美不卡一区二区三区免 | 欠操h | 二次元美女挤奶漫画 | 国产剧情一区 | 侮辱丰满美丽的人妻 | 91亚洲精品国产自在现线 | 9420高清视频在线观看网百度 | 韩国三级在线播放 | 第一次破苞h| 禁欲天堂 |