Spring Boot @ConfigurationProperties使用指导
1.简介
Spring Boot的一个非常有用的功能是外部化配置,并且可以轻松访问属性文件中定义的属性。
我们现在将详细地探索@ConfigurationProperties注释。
2.设置
本文使用相当标准的设置。我们首先在我们的pom.xml中添加spring-boot-starter-parent作为父项:
<!-- Inherit defaults from Spring Boot --> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent>
为了能够验证文件中定义的属性,我们还需要JSR-303的实现。hibernate-validator就是其中之一。我们也将它添加到我们的pom.xml中:
<dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>6.0.16.Final</version> </dependency>
3.简单属性
官方文档建议我们将配置属性隔离到单独的POJO中。那么让我们从这开始:
1 @Configuration 2 @PropertySource("classpath:configprops.properties") 3 @ConfigurationProperties(prefix = "mail") 4 public class ConfigProperties { 5 6 private String hostName; 7 private int port; 8 private String from; 9 10 // standard getters and setters 11 }
我们使用@Configuration,以便Spring在应用程序上下文中创建一个Spring bean。
我们还使用@PropertySource来定义属性文件的位置。否则,Spring使用默认位置(classpath:application.properties)。
@ConfigurationProperties最适用于具有相同前缀的分层属性。所以我们这里添加一个前缀mail。
Spring框架使用标准的Java bean设置器,因此我们需要为每个属性声明setter方法(用于为字段赋值)。
注意:如果我们不在POJO中使用@Configuration ,那么我们需要在主Spring应用程序类中添加@EnableConfigurationProperties(ConfigProperties.class) 以将属性绑定到POJO:
@SpringBootApplication @EnableConfigurationProperties(ConfigProperties.class) public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);} }
完事!Spring将自动绑定在我们的属性文件中定义的任何属性,该属性具有前缀mail并且后续字段与ConfigProperties类中的一个字段有相同的名称。
Spring使用一些宽松的绑定属性规则。因此以下变体都绑定到属性hostName:
mail.hostName mail.hostname mail.host_name mail.host-name mail.HOST_NAME
我们可以使用以下属性文件来设置所有字段:
#Simple properties mail.hostname=host@mail.com mail.port=9000 mail.from=mailer@mail.com
4.嵌套属性
我们可以在list、map和类中使用嵌套属性。
让我们创建一个新的Credentials类,用于一些嵌套属性:
public class Credentials {private String authMethod;private String username;private String password;// standard getters and setters }
我们还更新ConfigProperties类使用List,一个Map和Credentials 类:
public class ConfigProperties {private String host;private int port;private String from;private List<String> defaultRecipients;private Map<String, String> additionalHeaders;private Credentials credentials;// standard getters and setters }
以下属性文件将设置所有字段:
#Simple properties mail.hostname=mailer@mail.com mail.port=9000 mail.from=mailer@mail.com#List properties mail.defaultRecipients[0]=admin@mail.com mail.defaultRecipients[1]=owner@mail.com#Map Properties mail.additionalHeaders.redelivery=true mail.additionalHeaders.secure=true#Object properties mail.credentials.username=john mail.credentials.password=password mail.credentials.authMethod=SHA1
5.属性验证
@ConfigurationProperties使用JSR-303格式提供属性验证。这允许各种整洁的东西。
例如,让我们强制使用hostName属性:
@NotBlank private String hostName;
并且authMethod属性长度为1到4个字符:
@Length(max = 4, min = 1) private String authMethod;
和端口属性从1025到65536:
@Min(1025) @Max(65536) private int port;
最后,from属性必须与电子邮件地址格式匹配:
@Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$") private String from;
这有助于我们减少代码中的大量if - else条件,使其看起来更清晰简洁。
如果这些验证中的任何一个失败,那么主应用程序将无法启动,报错信息IllegalStateException。
Hibernate Validation框架使用标准的Java bean getter和setter,因此我们为每个属性声明getter和setter非常重要。
六,结论
我们已经探索了@ConfigurationProperties注释,并看到了它提供的一些方便的功能,如轻松绑定和Bean验证。
转载于:https://www.cnblogs.com/gc65/p/10618395.html
Spring Boot @ConfigurationProperties使用指导相关推荐
- Spring Boot(四)Spring Boot @ConfigurationProperties实例
Spring Boot @ConfigurationProperties实例 一 . ConfigurationProperties的使用 通常,我们使用@Value注释来逐个注入.propertie ...
- Spring Boot——@ConfigurationProperties与@Value的区别
引言 Spring Boot从配置文件中取值的方式有两种,一种是批量注入@ConfigurationProperties,另一种是单独注入@Value. 它们之间除了批量与单独取值的区别之外,还存在着 ...
- 2.SpringBoot学习(二)——Spring Boot ConfigurationProperties
1.简介 1.1 概述 Annotation for externalized configuration. Add this to a class definition or a @Bean met ...
- Spring Boot @ConfigurationProperties注解的使用
/*** @ProjectName traffic* @ClassName UrlInfo* @Desicription 获取yaml配置文件中的内容* @Author Zhang Xueliang* ...
- Spring Boot的Tomcat 启动详解
https://juejin.im/post/5a3273a451882575d42f68f9 在解读embeddedTomcat容器启动之前有几个要点需读懂 Spring Boot @Configu ...
- Spring Boot —— YAML配置文件
引言 首先,YAML并不是仅仅可以使用在Java项目中,它是一种类似于json结构的标记语言,可以为所有的编程语言服务.它强调更直观的层级表示,比较适合描述配置文件中的层级关系. Spring Boo ...
- Spring Boot Ajax实例(十六)
这篇博文主要用于新手学习Spring Boot,同时也记录自己学习的过程- 文章内容主要来源于易百教程 本文将展示如何使用jQuery.ajax将HTML表单请求发送到Spring REST API并 ...
- Spring boot 注解 ConfigurationProperties 的使用
原创自 第一勺金 00 最近在学习使用 spring boot.发现其中 @ConfigurationProperties这个注解使用的比较多.搜了比较多的文档都是英文,避免以后忘记,这里我也总结下它 ...
- yml语法规则 (5.spring boot配置文件注入@ConfigurationProperties)、配置文件处理器...
applicationi.properties application.yml 以数据为中心,比json xml更适合做配置文件 k: v 表示一对键值对 空格缩进控制层级关系,左对齐的一列数据都 ...
最新文章
- Source Insight中的多行注释
- java中ftp删除文件,Java 实现ftp 文件上传、下载和删除
- 中石油训练赛 - 围栏翻新(思维+贪心+差分)
- Shell else if mysql_Shell if else语句(详解版)
- 用小程序·云开发两天搭建mini论坛丨实战
- java tcp聊天程序_java实现基于Tcp的socket聊天程序
- qtreewidget点击空白处时取消以选项_手机APP自动续费,我们要如何取消?
- Python知识笔记总结
- webstorm运行的端口在哪看_webstorm(10.0.2)的端口号修改
- 在发送邮件HTML中,CSS等问题
- 3D点云数据标注工具推荐
- 高通骁龙200、400、600、800处理器规格大全
- tbslog乱码转换_tbslog乱码转换
- 所有程序员都应该知道的 6 个软件开发步骤
- 指令引用的内存不能为written怎么解决
- Linux运维养成记-磁盘管理LVM 逻辑卷
- 数学之美(Beauty Of Mathematics)
- 特斯拉公布马斯克去年薪酬状况:工资5.6万美元 期权激励超过22亿美元
- 全排列算法(字典序法、SJT Algorithm 、Heap‘s Algorithm)
- Xmy的第二次python(文件操作)