欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

真實業(yè)務場景

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

假設某一個jar包內封裝了DataSourceProperties

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

在jar包的Configuration中,某個@Bean的構造過程中引用了這個DataSourceProperties

public JdbcTemplate buildJdbcTemplate(DataSourceProperties dataSourceProperties) {
}

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

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

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

解決方案一

使用org.springframework.boot.context.properties.bind.Binder 從配置文件構建配置對象

@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() 似乎是重復的編碼方式?

解決方案二

使@Value注解能夠支持自動調用這段代碼 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()));
    }
}

項目啟動時,添加String to Object的轉換器,支持@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();
            }
        });
    }
}

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

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功能的詳細內容,更多關于Value支持對象類型的資料請關注腳本之家其它相關文章!

相關文章

  • SpringBoot全局異常捕獲處理實現(xiàn)方案

    SpringBoot全局異常捕獲處理實現(xiàn)方案

    這篇文章主要詳細介紹了SpringBoot全局異常捕獲處理實現(xiàn)方案,文章通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • Java中spring boot 字符串判斷是否為空方法小結

    Java中spring boot 字符串判斷是否為空方法小結

    這篇文章主要介紹了Java中spring boot字符串判斷是否為空,通過安裝依賴,結合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-11-11
  • java?9大性能優(yōu)化經(jīng)驗總結

    java?9大性能優(yōu)化經(jīng)驗總結

    這篇文章主要介紹了java?9大性能優(yōu)化經(jīng)驗總結,包括:Java代碼優(yōu)化,數(shù)據(jù)庫優(yōu)化,分布式緩存,異步化,Web前段,搜索引擎優(yōu)化等需要的朋友可以參考下
    2023-02-02
  • 解讀JDK、JRE、JVM的區(qū)別與聯(lián)系

    解讀JDK、JRE、JVM的區(qū)別與聯(lián)系

    這篇文章主要介紹了解讀JDK、JRE、JVM的區(qū)別與聯(lián)系,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 一文徹底弄懂零拷貝原理以及java實現(xiàn)

    一文徹底弄懂零拷貝原理以及java實現(xiàn)

    零拷貝(英語: Zero-copy) 技術是指計算機執(zhí)行操作時,CPU不需要先將數(shù)據(jù)從某處內存復制到另一個特定區(qū)域,下面這篇文章主要給大家介紹了關于零拷貝原理以及java實現(xiàn)的相關資料,需要的朋友可以參考下
    2021-08-08
  • Java 反射調用靜態(tài)方法的簡單實例

    Java 反射調用靜態(tài)方法的簡單實例

    下面小編就為大家?guī)硪黄狫ava 反射調用靜態(tài)方法的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • Java等待喚醒機制原理實例解析

    Java等待喚醒機制原理實例解析

    這篇文章主要介紹了Java等待喚醒機制原理實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • 基于Integer值判斷是否相等的問題

    基于Integer值判斷是否相等的問題

    這篇文章主要介紹了基于Integer值判斷是否相等的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 關于eclipse安裝spring插件報錯An error occurred while collecting items to be installed...解決方案

    關于eclipse安裝spring插件報錯An error occurred while collecting item

    這篇文章主要介紹了關于eclipse安裝spring插件報錯An error occurred while collecting items to be installed...解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Java反射機制如何解決數(shù)據(jù)傳值為空的問題

    Java反射機制如何解決數(shù)據(jù)傳值為空的問題

    這篇文章主要介紹了Java反射機制如何解決數(shù)據(jù)傳值為空的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論