Value注解支持对象类型ConfigurationProperties功能

目录
  • 真实业务场景
    • 解决方案一
    • 解决方案二

真实业务场景

(不希望配置类注册为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) {
}

在某个业务场景中,同时存在两个DataSourceProperties 会造成一个问题,注入的时候会提示有多个候选的bean 但是没法去修改Jar包中的内容

自己重复写一个DataSourceProperties 不是很优雅

这时候引出了一个需求,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();
            }
        });
    }
}

转换后代码执行 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支持对象类型的资料请关注我们其它相关文章!

(0)

相关推荐

  • 使用递归遍历对象获得value值的实现方法

    一般要用到递归,就要判断对象是否和父类型是否一样 这里演示简单的对象递归,还有数组递归类似. var obj = { a:{w:1,y:2,x:3}, b:{s:4,j:5,x:6}, c:{car:7,cat:8,mao:9} } function f(s){ for(var i in s){ if(typeof s[i]=="object"){ f(s[i]) }else{ console.log(s[i]); } } } f(obj); 返回结果:1,2,3,4,5,6,7,8,

  • 聊聊@value注解和@ConfigurationProperties注解的使用

    目录 @value注解和@ConfigurationProperties注解 @value读取默认配置 @ConfigurationProperties读取默认配置 @ConfigurationProperties和@Value使用上的一点区别 第一段代码 第二段代码 @value注解和@ConfigurationProperties注解 @value读取默认配置 yml文件内容如下(装了STS插件以后即可直接使用,改后缀就行了) user: username: xiongda sex: man

  • SpringBoot中注解@ConfigurationProperties与@Value的区别与使用详解

    目录 注解@ConfigurationProperties 注解@Value 区别 松散语法绑定: SpEl语法表示: JSR303数据校验: 复杂类型封装: 配置文件注入值数据校验 注解@ConfigurationProperties 该注解的作用是将配置文件中配置的每一个属性的值,映射到这个组件中.@ConfigurationProperties :告诉springboot将本类中的所有属性和配置文件中相关的配置进行绑定 prefix = "person":配置文件中哪个下面的所有

  • spring框架下@value注解属性static无法获取值问题

    目录 @value注解属性static无法获取值 解决办法 @Value注解取不到值的几种情况 几种获取不到值的特殊情况如下 @value注解属性static无法获取值 @Value("${appId}") private static String appid; 这样是无法直接获得值的 解决办法 需要这样写 private static String appid; @Value("${appId}") public void setAppid(String app

  • springboot如何通过@Value,@ConfigurationProperties获取配置

    目录 通过@Value,@ConfigurationProperties获取配置 spring boot 获取配置项值 通过@Value 获取值 通过@ConfigurationProperties 获取值 说下@ConfigurationProperties和@Value区别 配置文件注入值数据校验 通过@Value,@ConfigurationProperties获取配置 spring boot 获取配置项值 使用版本是1.5.4 举例一个线程池的配置: 在application.yml添加

  • Spring如何利用@Value注解读取yml中的map配置

    目录 @Value注解读取yml中的map配置 下边是我在yml中的map写法 使用时候注解的写法 举个例子 spring注解@Value通过yml文件注入map yml文件 java代码注入 @Value注解读取yml中的map配置 网上查了好多资料,都是.properties文件中读取,而且又是几个人抄来抄去,找了半天功夫不负有心人,终于找到了详尽的用法介绍. 下边是我在yml中的map写法 test:   map: '{"test1":"12345",&quo

  • Value注解支持对象类型ConfigurationProperties功能

    目录 真实业务场景 解决方案一 解决方案二 真实业务场景 (不希望配置类注册为Bean 或 不希望声明@ConfigurationProperties) 假设某一个jar包内封装了DataSourceProperties @Configuration @ConfigurationProperties( prefix = "my.datasource" ) @Data public class DataSourceProperties { private List<String&g

  • Android用注解与反射实现Butterknife功能

    目录 自定义注解 使用自定义注解 通过反射机制获取注解参数 1. 布局文件获取 2. 控件获取实现 3. 控件点击响应 自定义注解 1) 先定义布局文件注入 //注解的作用域在类上 @Target(ElementType.TYPE) //让保持性策略为运行时态,将注解编码到class文件中,让虚拟机读取 @Retention(RetentionPolicy.RUNTIME) public @interface ContentView { int value();//使用时直接@ContentVi

  • 详解JavaScript对象类型

    JavaScrtip有六种数据类型,一种复杂的数据类型(引用类型),即Object对象类型,还有五种简单的数据类型(原始类型):Number.String.Boolean.Undefined和Null.其中,最核心的类型就是对象类型了.同时要注意,简单类型都是不可变的,而对象类型是可变的.  什么是对象  一个对象是一组简单数据类型(有时是引用数据类型)的无序列表,被存储为一系列的名-值对(name-value pairs).这个列表中的每一项被称为 属性(如果是函数则被称为 方法).  下面是

  • 基于jQuery下拉选择框插件支持单选多选功能代码

    由于最近项目的需求,需要做一个下拉选择框的插件,支持单选显示表单数据,多选显示表格数据,该插件主要运用了jQuery与jqgrid以及easyui. 下面给大家展示下效果图,如果大家感觉还不错,请参考实现代码: 多选:呈现列表 具体代码如下所示: /** *下拉框插件-chooseList *调用插件的方式以及格式: * 1.首先你需要创建一个div面板,给div定义ID * 2.在你所需要的地方调用插件: * 参数说明: * $("#divID").chooseList({ * qu

  • Python入门篇之对象类型

    Python使用对象模型来存储数据.构造任何类型的值都是一个对象 所有的Python对象都拥有三个特性:身份.类型.值 身份: 每一个对象都有一个唯一的身份来标志自己,任何对象的身份可以使用内建函数id()来得到.这个值可以被认为是该对象的内存地址 类型: 对象的类型决定了该对象可以保存什么类型的值,可以进行怎样的操作,以及遵循什么样的规则,可以使用内建函数type()查看Python对象的类型: 复制代码 代码如下: >>> type([1,2]) <type 'list'>

  • Mybatis基于注解实现多表查询功能

    对应的四种数据库表关系中存在四种关系:一对多,多对应,一对一,多对多.在前文中已经实现了xml配置方式实现表关系的查询,本文记录一下Mybatis怎么通过注解实现多表的查询,算是一个知识的补充. 同样的先介绍一下Demo的情况:存在两个实体类用户类和账户类,用户类可能存在多个账户,即一对多的表关系.每个账户只能属于一个用户,即一对一或者多对一关系.我们最后实现两个方法,第一个实现查询所有用户信息并同时查询出每个用户的账户信息,第二个实现查询所有的账户信息并且同时查询出其所属的用户信息. 1.项目

  • 让你的Python代码实现类型提示功能

    Python是一种动态类型语言,这意味着我们在编写代码的时候更为自由,但是与此同时IDE无法向静态类型语言那样分析代码,及时给我们相应的提示.为了解决这个问题,Python 3.6 新增了几个特性PEP 484和PEP 526,帮助编辑器为我们提供更智能的提示.这些新特性不会影响语言本身,只是增加一点提示.当你使用比较智能的开发工具比如PyCHarm时,就会感觉到类型提示的方便之处. 变量注解 首先先看看变量注解.它的语法和某些类型后置的语言类似. # 变量注解 a: int = 5 b: bo

  • 不用typsescript如何使用类型增强功能

    前言 由于 JS 的弱类型.宽松的编写规范.以及开发工具的弱鸡支持,我们在维护前人的代码时,经常会出现不知道某一个方法或字段命名来自于哪里,一定要在全局搜索以后慢慢筛查才能找到 同样我们在使用接口返回的对象字段时,也不知其类型几何,意思几何 甚至在我们使用挂载到 vue 全局对象上的方法时,纯粹靠猜,尤其是当函数可以接收多种类型的时候,很痛苦 先说目的:我们希望一切资源皆可索引到其定义或来源,可以有代码补全,在vscode内ctr+鼠标左键皆可导航到,提高效率,用的爽 具体配置信息 配置全局js

  • SpringData如何通过@Query注解支持JPA语句和原生SQL语句

    目录 通过@Query注解支持JPA语句和原生SQL语句 @Query注解的用法(Spring Data JPA) 1.一个使用@Query注解的简单例子 2.Like表达式 3.使用Native SQL Query 4.使用@Param注解注入参数 5.SPEL表达式(使用时请参考最后的补充说明) 6.一个较完整的例子 7.解释例6中错误的原因 通过@Query注解支持JPA语句和原生SQL语句 在SpringData中们可是使用继承接口直接按照规则写方法名即可完成查询的方法,不需要写具体的实

  • Python自定义对象实现切片功能

    目录 1.魔术方法:__getitem__() 2.自定义序列实现切片功能 3.自定义字典实现切片功能 切片是 Python 中最迷人最强大最 Amazing 的语言特性(几乎没有之一),在<Python进阶:切片的误区与高级用法>中,我介绍了切片的基础用法.高级用法以及一些使用误区.这些内容都是基于原生的序列类型(如字符串.列表.元组--),那么,我们是否可以定义自己的序列类型并让它支持切片语法呢?更进一步,我们是否可以自定义其它对象(如字典)并让它支持切片呢? 1.魔术方法:__getit

随机推荐