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支持對象類型的資料請關注腳本之家其它相關文章!
相關文章
Java中spring boot 字符串判斷是否為空方法小結
這篇文章主要介紹了Java中spring boot字符串判斷是否為空,通過安裝依賴,結合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11關于eclipse安裝spring插件報錯An error occurred while collecting item
這篇文章主要介紹了關于eclipse安裝spring插件報錯An error occurred while collecting items to be installed...解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08Java反射機制如何解決數(shù)據(jù)傳值為空的問題
這篇文章主要介紹了Java反射機制如何解決數(shù)據(jù)傳值為空的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03