• Spring Boot中的Binder类


    介绍

    Spring Boot中的Binder类是一个用于绑定属性的工具类。它可以将配置文件中的属性值绑定到Java对象中,从而方便地进行配置管理。


    简单示例

    import org.springframework.boot.context.properties.bind.Binder;
    import org.springframework.core.env.Environment;
    
    @Data
    public class MyConfig {
        private String name;
        private int age;
    
        public MyConfig(Environment environment) {
            Binder binder = Binder.get(environment);
            this.name = binder.bind("myconfig.name", String.class).orElse("lucifer");
            this.age = binder.bind("myconfig.age", Integer.class).orElse(25);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    使用Binder类将配置文件中的属性值绑定到这些属性中。在构造函数中,首先获取了一个Binder实例,然后使用bind方法将配置文件中的属性值绑定到Java对象中。如果属性不存在,则使用默认值。


    配置文件中属性:

    myconfig.name=John
    myconfig.age=25
    
    • 1
    • 2

    需要将Environment对象传递给MyConfig的构造函数,以便Binder类可以访问配置文件中的属性值。在Spring Boot应用程序中,可以通过@Autowired注解将Environment对象注入到MyConfig类中。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.Environment;
    
    @Configuration
    public class AppConfig {
        @Autowired
        private Environment environment;
    
        public MyConfig myConfig() {
            return new MyConfig(environment);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    常用方法

    • bind方法:将配置文件中的属性值绑定到Java对象中。
    @ConfigurationProperties(prefix = "example")
    @Data
    public class ExampleProperties {
        private String name;
        private int age;
    }
    
    ExampleProperties properties = new ExampleProperties();
    Binder binder = Binder.get(environment);
    binder.bind("example", Bindable.ofInstance(properties));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • bindOrCreate方法:如果Java对象不存在,则创建一个新的对象并将配置文件中的属性值绑定到该对象中。
    ExampleProperties properties = Binder.get(environment)
            .bindOrCreate("example", Bindable.of(ExampleProperties.class));
    
    • 1
    • 2
    • bindProperty方法:将配置文件中的单个属性值绑定到Java对象的属性中。
    ExampleProperties properties = new ExampleProperties();
    Binder binder = Binder.get(environment);
    binder.bindProperty("example.name", Bindable.ofInstance(properties), String.class);
    
    • 1
    • 2
    • 3
    • bindAnnotations方法:将Java对象中带有@ConfigurationProperties注解的属性绑定到配置文件中的属性值。
    @Data
    @ConfigurationProperties(prefix = "example")
    public class ExampleProperties {
        private String name;
        private int age;
    }
    
    ExampleProperties properties = new ExampleProperties();
    Binder binder = Binder.get(environment);
    binder.bindAnnotations(properties);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
    C#进阶高级程序员开发必知必会:泛型的定义实操案例: 实现堆栈的后进先出功能
    成都瀚网科技有限公司:抖店的评论会消失吗?
    如何设计实现系统应支持至少300个并行用户的同时访问和使用的需求
    Mysql应用日志时间与系统时间相差八小时
    MFC Windows 程序设计[340]之特效按钮的实现(附源码)
    第二章 Hadoop环境配置之虚拟机安装配置
    供应脂质体形成材料DSPE-PEG-Thiol,DSPE-PEG-SH
    Vue 3.0 + vite + axios+PHP跨域问题的解决办法
    python实现办公自动化读书笔记——自动化处理PDF文件
  • 原文地址:https://blog.csdn.net/weixin_42594143/article/details/133269692