最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Value注解支持對象類型ConfigurationProperties功能

 更新時(shí)間:2022年10月24日 15:50:03   作者:uhfun  
這篇文章主要為大家介紹了Value注解支持對象類型ConfigurationProperties功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

真實(shí)業(yè)務(wù)場景

(不希望配置類注冊為Bean 或 不希望聲明@ConfigurationProperties)

假設(shè)某一個(gè)jar包內(nèi)封裝了DataSourceProperties

@Configuration
@ConfigurationProperties(
    prefix = "my.datasource"
)
@Data
public class DataSourceProperties {
    private List<String> suffix;
    private List<DataSourceDetailProperties> db;
}

在jar包的Configuration中,某個(gè)@Bean的構(gòu)造過程中引用了這個(gè)DataSourceProperties

public JdbcTemplate buildJdbcTemplate(DataSourceProperties dataSourceProperties) {
}

在某個(gè)業(yè)務(wù)場景中,同時(shí)存在兩個(gè)DataSourceProperties 會造成一個(gè)問題,注入的時(shí)候會提示有多個(gè)候選的bean 但是沒法去修改Jar包中的內(nèi)容

自己重復(fù)寫一個(gè)DataSourceProperties 不是很優(yōu)雅

這時(shí)候引出了一個(gè)需求,DataSourceProperties不希望注冊為Bean,但是能夠從配置文件讀取構(gòu)建對象

解決方案一

使用org.springframework.boot.context.properties.bind.Binder 從配置文件構(gòu)建配置對象

@Bean
public JdbcTemplate buildJdbcTemplate(Environment environment) {
     Binder binder = Binder.get(environment);
     DataSourceProperties
                properties1 = binder.bind("my.datasource1", Bindable.of(DataSourceProperties.class)).get(),
                properties2 = binder.bind("my.datasource2", Bindable.of(DataSourceProperties.class)).get();
}

binder.bind("xxx", Bindable.of(type)).get() 似乎是重復(fù)的編碼方式?

解決方案二

使@Value注解能夠支持自動(dòng)調(diào)用這段代碼 binder.bind("xxx", Bindable.of(type)).get() 例如

@Bean
public JdbcTemplate buildJdbcTemplate(@Value("my.datasource1") DataSourceProperties properties1,
                                      @Value("my.datasource2") DataSourceProperties properties2) {   
}

org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency 最后會交由converter處理

Class<?> type = descriptor.getDependencyType();
Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor);
if (value != null) {
    if (value instanceof String) {
        String strVal = resolveEmbeddedValue((String) value);
        BeanDefinition bd = (beanName != null && containsBean(beanName) ?
                getMergedBeanDefinition(beanName) : null);
        value = evaluateBeanDefinitionString(strVal, bd);
    }
    TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
    try {
        return converter.convertIfNecessary(value, type, descriptor.getTypeDescriptor());
    }
    catch (UnsupportedOperationException ex) {
        // A custom TypeConverter which does not support TypeDescriptor resolution...
        return (descriptor.getField() != null ?
                converter.convertIfNecessary(value, type, descriptor.getField()) :
                converter.convertIfNecessary(value, type, descriptor.getMethodParameter()));
    }
}

項(xiàng)目啟動(dòng)時(shí),添加String to Object的轉(zhuǎn)換器,支持@Value 并且 "bind:"開頭(防止影響@Value原有功能)

package com.nuonuo.accounting.guiding.support.spring;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.convert.ApplicationConversionService;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import java.util.Set;
import static java.util.Collections.singleton;
/**
 * @author uhfun
 */
public class ValuePropertiesBindableAnnotationSupport implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    private static final String PREFIX = "bind:";
    @Override
    public void initialize(ConfigurableApplicationContext context) {
        Binder binder = Binder.get(context.getEnvironment());
        ((ApplicationConversionService) context.getBeanFactory().getConversionService()).addConverter(new ConditionalGenericConverter() {
            @Override
            public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
                Value value = targetType.getAnnotation(Value.class);
                return value != null && value.value().startsWith(PREFIX);
            }
            @Override
            public Set<ConvertiblePair> getConvertibleTypes() {
                return singleton(new ConvertiblePair(String.class, Object.class));
            }
            @Override
            public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
                Value value = targetType.getAnnotation(Value.class);
                Class<?> type = targetType.getType();
                assert value != null;
                return binder.bind(value.value().replace(PREFIX, ""), Bindable.of(type)).get();
            }
        });
    }
}

轉(zhuǎn)換后代碼執(zhí)行 binder.bind(value.value().replace(PREFIX, ""), Bindable.of(type)).get(); 目的就達(dá)成了

META-INF/spring.factories中添加注冊的Bean

# ApplicationContextInitializer
org.springframework.context.ApplicationContextInitializer=\
com.nuonuo.accounting.guiding.support.spring.ValuePropertiesBindableAnnotationSupport,\

最終效果

@Bean
public JdbcTemplate buildJdbcTemplate(@Value("bind:my.datasource1") DataSourceProperties properties1,
                                      @Value("bind:my.datasource2") DataSourceProperties properties2) {   
}

以上就是Value注解支持對象類型ConfigurationProperties功能的詳細(xì)內(nèi)容,更多關(guān)于Value支持對象類型的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 圖解Java經(jīng)典算法冒泡排序的原理與實(shí)現(xiàn)

    圖解Java經(jīng)典算法冒泡排序的原理與實(shí)現(xiàn)

    冒泡排序是一種簡單的排序算法,它也是一種穩(wěn)定排序算法。其實(shí)現(xiàn)原理是重復(fù)掃描待排序序列,并比較每一對相鄰的元素,當(dāng)該對元素順序不正確時(shí)進(jìn)行交換。一直重復(fù)這個(gè)過程,直到?jīng)]有任何兩個(gè)相鄰元素可以交換,就表明完成了排序
    2022-09-09
  • Spring入門到精通之注解開發(fā)詳解

    Spring入門到精通之注解開發(fā)詳解

    Spring是輕代碼而重配置的框架,配置比較繁重,影響開發(fā)效率,所以注解開發(fā)是一種趨勢。本文將通過示例為大家詳細(xì)講講Spring如何實(shí)現(xiàn)注解開發(fā),感興趣的可以學(xué)習(xí)一下
    2022-07-07
  • SpringBoot整合MyBatis實(shí)現(xiàn)CRUD操作項(xiàng)目實(shí)踐

    SpringBoot整合MyBatis實(shí)現(xiàn)CRUD操作項(xiàng)目實(shí)踐

    本文主要介紹了SpringBoot整合MyBatis實(shí)現(xiàn)CRUD操作項(xiàng)目實(shí)踐,如何實(shí)現(xiàn)數(shù)據(jù)庫的CRUD創(chuàng)建、讀取、更新、刪除操作,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • Java中println輸出漢字亂碼問題一招解決方案

    Java中println輸出漢字亂碼問題一招解決方案

    這篇文章主要介紹了Java中println輸出漢字亂碼問題一招解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java計(jì)算一個(gè)數(shù)加上100是完全平方數(shù),加上168還是完全平方數(shù)

    Java計(jì)算一個(gè)數(shù)加上100是完全平方數(shù),加上168還是完全平方數(shù)

    這篇文章主要介紹了Java計(jì)算一個(gè)數(shù)加上100是完全平方數(shù),加上168還是完全平方數(shù),需要的朋友可以參考下
    2017-02-02
  • 深入理解java中for和foreach循環(huán)

    深入理解java中for和foreach循環(huán)

    下面小編就為大家?guī)硪黄钊肜斫鈐ava中for和foreach循環(huán)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-07-07
  • Seata之分布式事務(wù)問題及解決方案

    Seata之分布式事務(wù)問題及解決方案

    這篇文章主要介紹了Seata之分布式事務(wù)問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • IDEA修改idea64.exe.vmoptions文件以及解決coding卡頓問題

    IDEA修改idea64.exe.vmoptions文件以及解決coding卡頓問題

    IDEA修改idea64.exe.vmoptions文件以及解決coding卡頓問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • RabbitMQ實(shí)現(xiàn)消費(fèi)端限流的步驟

    RabbitMQ實(shí)現(xiàn)消費(fèi)端限流的步驟

    消費(fèi)者端限流的主要目的是控制消費(fèi)者每次從 RabbitMQ 中獲取的消息數(shù)量,從而實(shí)現(xiàn)消息處理的流量控制,這篇文章主要介紹了RabbitMQ如何實(shí)現(xiàn)消費(fèi)端限流,需要的朋友可以參考下
    2024-03-03
  • 使用JWT創(chuàng)建解析令牌及RSA非對稱加密詳解

    使用JWT創(chuàng)建解析令牌及RSA非對稱加密詳解

    這篇文章主要介紹了JWT創(chuàng)建解析令牌及RSA非對稱加密詳解,JWT是JSON Web Token的縮寫,即JSON Web令牌,是一種自包含令牌,一種情況是webapi,類似之前的阿里云播放憑證的功能,另一種情況是多web服務(wù)器下實(shí)現(xiàn)無狀態(tài)分布式身份驗(yàn)證,需要的朋友可以參考下
    2023-11-11

最新評論

固安县| 乌恰县| 新邵县| 景德镇市| 民权县| 德清县| 女性| 卢氏县| 岑溪市| 辉南县| 绍兴市| 扎鲁特旗| 台江县| 阿瓦提县| 敖汉旗| 富锦市| 建阳市| 茂名市| 连南| 五大连池市| 隆化县| 阳曲县| 红河县| 双牌县| 沅江市| 潜山县| 文山县| 内江市| 灌云县| 佛山市| 库尔勒市| 习水县| 集贤县| 江孜县| 商水县| 临夏市| 烟台市| 吉木乃县| 临澧县| 西宁市| 手游|