关于springframework的binder类绑定配置文件属性

··近期因为公司有个需求做配置文件加解密,参数加解密可以直接用jasyp来做,但是键的加密需要自己完成,最一开始是新建一个固定配置文件来存储新旧键值比如(abc=username);这样其他配置文件的usename都要改成abc,使用的方法是实现Springapplicationrunlistener,在contexloaded阶段获取全局环境变量Environment读取所有配置文件列表,循环读取文件中的键值对去比较固定配置文件中的键值对去修改键,然后再放回去做更改;

··后来领导说度固定配置文件太死了,需要放到所有spring可加载的配置文件中去读取,这时候因为不再固定文件所以需要自定义一个前缀去读取,但是因为可配置所以是一个集合类似于

properties中的配置
test.test.model[0].key
test.test.model[0].value
test.test.model[1].key
test.test.model[1].value
yml中的话是:
test:
  test:
    model:
      - key: abc
        value: username
      - key: dfg
        value: password

//这时候通过env读取properties 获取到的属性都会是
//test.test.model[0].key
//test.test.model[0].value
//test.test.model[1].key
//test.test.model[1].value
//这种形式  需要自己转换成model对象集合的话比较费劲
//model对象
public class Model{
    private String key;
    private string value;
}

我有尝试过使用:

@ConfigurationProperties(prefix="test.test")
public class test{
    public List<model> models;
}

注解来用spring自动注入属性但是没生效,获取到的models在进入到我这个阶段断点的时候是null,但是在容器启动起来之后就会有值,因为对底层没太深研究所以猜测是因为在contextLoaded阶段spring还无法进行属性配置,所以我只能手动匹配到model对象上去;

所以我去寻找解决办法,找了好久找到了这个Binder的类;

他可以绑定配置文件属性到对象、集合、map上去;

废话不多说上代码:

获取env
private final ConfigurableEnviroment environment;
//获取binder对象
Binder binder=Binder.get(environment);
//把配置文件中的某个属性绑定到对应对象上例如:
//spring.datasource.url
//spring.datasource.username
//spring.datasource.password
//就会把这三个属性绑定到datasource对象的三个属性上
Datasource datasoure=binder.bind("spring.datasource",Datasource.class).get();
//test.test.model[0].id
//test.test.model[0].name
//test.test.model[1].id
//test.test.model[1].name 
//model的数组对象转换为list集合对象
List<model> models=binder.bind( "test.test.model",Bindable.listOf(Model.class) ).get();
//把配置文件中的某个对象转为map键值对类似于对象
//spring.datasource.url
//spring.datasource.username
//spring.datasource.password
//就会把这三个属性换为map的键值对
Map<String,Object> propMap=binder.bind("spring.datasource",Bindable.mapOf(String.class, Object.class).get();

 很方便,不需要自己苦哈哈的处理test.test.model[0].id这种数据。

希望对你们有帮助,我自己也做个记录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
main SpringApplication.java:771 - Application startup failed org.springframework.context.ApplicationContextException: Failed to start bean 'inputBindingLifecycle'; nested exception is org.springframework.cloud.stream.binder.BinderException: Exception thrown while starting consumer: at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50) at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348) at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151) at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114) at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:880) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) at com.migu.rstone.UserCenterApplication.main(UserCenterApplication.java:32) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) Caused by: org.springframework.cloud.stream.binder.BinderException: Exception thrown while starting consumer: at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.doBindConsumer(AbstractMessageChannelBinder.java:258) at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.doBindConsumer(AbstractMessageChannelBinder.java:57) at org.springframework.cloud.stream.binder.AbstractBinder.bindConsumer(AbstractBinder.java:145) at org.springframework.cloud.stream.binding.BindingService.bindConsumer(BindingService.java:97) at org.springframework.cloud.stream.binding.BindableProxyFactory.bindInputs(BindableProxyFactory.java:221) at org.springframework.cloud.stream.binding.InputBindingLifecycle.start(InputBindingLifecycle.java:55) at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:175) ... 22 more Caused by: java.lang.IllegalArgumentException: A list of partitions must be provided at org.springframework.util.Assert.isTrue(Assert.java:92) at org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder.createConsumerEndpoint(KafkaMessageChannelBinder.java:241) at org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder.createConsumerEndpoint(KafkaMessageChannelBinder.java:88) at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.doBindConsumer(AbstractMessageChannelBinder.java:217) ... 28 more 报错解决办法
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值