Spring Boot中的Binder类

介绍

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

简单示例

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);
    }
}

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

配置文件中属性:

java 复制代码
myconfig.name=John
myconfig.age=25

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

java 复制代码
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);
    }
}

常用方法

  • bind方法:将配置文件中的属性值绑定到Java对象中。
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));
  • bindOrCreate方法:如果Java对象不存在,则创建一个新的对象并将配置文件中的属性值绑定到该对象中。
java 复制代码
ExampleProperties properties = Binder.get(environment)
        .bindOrCreate("example", Bindable.of(ExampleProperties.class));
  • bindProperty方法:将配置文件中的单个属性值绑定到Java对象的属性中。
java 复制代码
ExampleProperties properties = new ExampleProperties();
Binder binder = Binder.get(environment);
binder.bindProperty("example.name", Bindable.ofInstance(properties), String.class);
  • bindAnnotations方法:将Java对象中带有@ConfigurationProperties注解的属性绑定到配置文件中的属性值。
java 复制代码
@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);
相关推荐
czlczl200209254 分钟前
SpringBoot中web请求路径匹配的两种风格
java·前端·spring boot
bigdata-rookie7 分钟前
Scala 泛型
开发语言·后端·scala
开心猴爷8 分钟前
HTTPS Everywhere 时代的抓包挑战,从加密流量解析到底层数据流捕获的全流程方案
后端
Q_Q5110082859 分钟前
python+django/flask医药垃圾分类管理系统
spring boot·python·django·flask·node.js·php
卓码软件测评15 分钟前
【第三方CNAS软件测试机构:Gatling中的资源监控_实时收集服务器CPU、内存、磁盘I/O和网络指标】
后端·测试工具·测试用例·scala·压力测试
DengRan16 分钟前
别再手写 Java 二进制解析了 — 用 FastProto 从繁琐到优雅
后端
不是笨小孩13518 分钟前
多元算力融合实践:openEuler在中等配置硬件环境下的性能验证
后端
q_191328469533 分钟前
基于SpringBoot2+Vue2+uniapp的考研社区论坛网站及小程序
java·vue.js·spring boot·后端·小程序·uni-app·毕业设计
稚辉君.MCA_P8_Java40 分钟前
Gemini永久会员 深度解析jvm内存结构
jvm·后端·架构
武子康42 分钟前
大数据-174 Elasticsearch 查询 DSL 实战:match/match_phrase/query_string/multi_match 全解析
大数据·后端·elasticsearch