本文主要是介绍springboot Binder类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
比Environment类好用很多,可以非常方便地进行类型转换,可以将属性绑定到对象,Map,List等类型上
# 绑定到对象MailPropertiesC propertiesC = Binder.get(environment) //首先要绑定配置器//再将属性绑定到对象上.bind( "kaka.cream.mail-c", Bindable.of(MailPropertiesC.class) ).get(); //再获取实例# 绑定MapMap<String,Object> propMap = Binder.get(environment).bind( "fish.jdbc.datasource",Bindable.mapOf(String.class, Object.class) ).get();# 绑定ListList<String> list = Binder.get(environment).bind( "kaka.cream.list",Bindable.listOf(String.class) ).get();
例如:
// 绑定的类
public class MultiKafkaProperties {private Map<String, Properties> producer;private Map<String, Properties> consumer;// getter// setter
}
# 配置
kafka.producer.server=127.0.0.1:9093
kafka.producer.username=kafka
kafka.producer.password=password
kafka.producer.topic=topic1kafka.consumer.server=127.0.0.1:9093
kafka.consumer.username=kafka
kafka.consumer.password=password
kafka.consumer.topic=topic1
kafka.consumer.group.id=group1
kafka.consumer.poll.ms=3000
kafka.consumer.max.poll.records=300
// 绑定
Binder binder = Binder.get(environment);
MultiKafkaProperties config = binder.bind("kafka", MultiKafkaProperties.class).get();
这篇关于springboot Binder类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!