当前位置: 首页 > news >正文

推荐一个免费网站成都网站设计制作工作室

推荐一个免费网站,成都网站设计制作工作室,建设商务网站公司,学做网站论坛课程介绍 Spring Boot中的Binder类是一个用于绑定属性的工具类。它可以将配置文件中的属性值绑定到Java对象中,从而方便地进行配置管理。 简单示例 import org.springframework.boot.context.properties.bind.Binder; import org.springframework.core.env.Environmen…

介绍

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

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


配置文件中属性:

myconfig.name=John
myconfig.age=25

需要将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 {@Autowiredprivate Environment environment;public MyConfig myConfig() {return new MyConfig(environment);}
}

常用方法

  • 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));
  • bindOrCreate方法:如果Java对象不存在,则创建一个新的对象并将配置文件中的属性值绑定到该对象中。
ExampleProperties properties = Binder.get(environment).bindOrCreate("example", Bindable.of(ExampleProperties.class));
  • bindProperty方法:将配置文件中的单个属性值绑定到Java对象的属性中。
ExampleProperties properties = new ExampleProperties();
Binder binder = Binder.get(environment);
binder.bindProperty("example.name", Bindable.ofInstance(properties), String.class);
  • 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);
http://www.15wanjia.com/news/183164.html

相关文章:

  • 用ps怎么做网站首页东莞外贸公司网站建设
  • 下载网站上的表格 怎么做黄石网站建设哪家好
  • 简单网站制作步骤网站开发报价标准
  • 网站做推广团队微信第五条广告怎么做
  • 如何分析一个网站的用户网站改版模版
  • 如何用织梦cms做网站企业邮箱登录入口首页
  • 如何做网站logo 设置平滑一个正规平台维护多久
  • 苏州公司网站建设报价兰州百度网页制作公司
  • 网站如何换域名软件工程师40岁后的出路
  • 山东住房建设厅官网站网站做宣传
  • 中国建设银行网站口长沙专业建网站公司
  • 三门峡网站开发语文建设 官方网站
  • 湖北省建设厅官方网站八大员wordpress还是帝国cms对比
  • 关于网站开发人员保密协议北京网站优化推广收集
  • wordpress多站点不同主题创新作品及其设计方案
  • 查重网站开发网站代备案公司名称
  • 中小企业网站功能wordpress手机博客主题
  • 上网建立网站布置旅游网站建设流程步骤
  • 果洛营销网站建设哪家好常州市城市建设局网站
  • 个人网站花多少钱房地产最新消息政策代表了什么
  • 网站建设 psd17网站一起做网店潮汕依依
  • 怎么做公司内部网站游戏网站平台
  • 做营销型网站的公司做视频网站公司要怎么做的
  • 公司建网站需要先注册域名教育网站官网
  • 推介网手机版怎样做seo搜索引擎优化
  • 如何做一个购物网站班级网站建设模板下载
  • 邯郸市建设局官方网站湖北建设信息网站 联系方式
  • 学校网站平台建设方案河南企业建设网站
  • 梧州网站建设厂家网站推广含义
  • 文化馆网站建设地推团队联系方式