Spring Boot 揭秘与实战(四) 配置文件篇 - 有哪些很棒的特性
文章目录
Spring 框架本身提供了多种的方式来管理配置属性文件。Spring 3.1 之前可以使用 PropertyPlaceholderConfigurer。Spring 3.1 引入了新的环境(Environment)和概要信息(Profile)API,是一种更加灵活的处理不同环境和配置文件的方式。不过 Spring 这些配置管理方式的问题在于选择太多,让开发人员无所适从。Spring Boot 提供了一种统一的方式来管理应用的配置。
使用属性文件
自定义属性
Spring Boot 提供的 SpringApplication 类会搜索并加载 application.properties 文件来获取配置属性值。
创建 application.properties 文件。
- author.realname=梁桂钊
- author.nickname=LiangGzone
不需要其他配置,我们只需要通过 @Value(“${属性名}”) 注解来加载对应的配置属性,现在,通过单元测试用例来验证吧。
- @Value("${author.realname}")
- private String realname;
- @Value("${author.nickname}")
- private String nickname;
- @Test
- public void test1() throws Exception {
- System.out.println("real_name : " + realname);
- System.out.println("nick_name : " + nickname);
- }
参数引用
此外,我们来可以通过引用参数来使用。
- author.product=Spring Boot 揭秘与实战
- author.project=springboot-action
- author.intro=${author.product} | ${author.project} | 作者:${author.realname}
那么,问题来了, author.intro 通过参数引用得到的结果是什么呢?现在,通过单元测试用例来进行测试。
- @Value("${author.intro}")
- private String intro;
- @Test
- public void test2() throws Exception {
- System.out.println("intro : " + intro);
- }
随机数属性
Spring Boot 的属性配置文件中 ${random} 可以用来生成各种不同类型的随机值,从而简化了代码生成的麻烦,例如 生成 int 值、long 值或者 string 字符串。
- # 32位随机字符串
- rand.str = ${random.value}
- # 随机int类型
- rand.intid = ${random.int}
- # 随机long类型
- rand.longid = ${random.long}
- # 100以内的随机int类型
- rand.number = ${random.int(100)}
- # 0-100范围内的随机int类型
- rand.range = ${random.int[0,100]}
附上,单元测试用例。
- @Value("${rand.str}")
- private String randStr;
- @Value("${rand.intid}")
- private int randIntid;
- @Value("${rand.longid}")
- private long randLongid;
- @Value("${rand.number}")
- private int randNumber;
- @Value("${rand.range}")
- private String randRange;
- @Test
- public void test3() throws Exception {
- System.out.println("rand.str : " + randStr);
- System.out.println("rand.intid : " + randIntid);
- System.out.println("rand.longid : " + randLongid);
- System.out.println("rand.number : " + randNumber);
- System.out.println("rand.range : " + randRange);
- }
application-{profile}.properties参数加载
一个非常典型的场景,多环境配置。Spring Boot 也给我们提供了非常简化的配置。
现在,我们根据环境创建4个配置文件。
- #开发环境
- application-development.properties
- #测试环境
- application-test.properties
- #预生产环境
- application-preproduction.properties
- #生产环境
- application-product.properties
执行命令,通过 active 加载测试环境的配置。
- java -jar ***.jar --spring.profiles.active=test
YAML文件
相对于属性文件,YAML 文件是一个更好的配置文件格式。Spring Boot 提供的 SpringApplication 类也提供了对 YAML 配置文件的支持。
创建application.yml 文件
- author:
- email: lianggzone@163.com
- blog: http://blog.720ui.com
那么,我们再来测试一下,是否正常使用哈。
- @Value("${author.email}")
- private String email;
- @Value("${author.blog}")
- private String blog;
- @Test
- public void test4() throws Exception {
- System.out.println("email : " + email);
- System.out.println("blog : " + blog);
- }
源代码
相关示例完整代码: springboot-action
(完)
如果觉得我的文章对你有帮助,请随意打赏。

- 版权声明:本文由 梁桂钊 发表于 梁桂钊的博客
- 转载声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证),非商业转载请注明作者及出处,商业转载请联系作者本人。
- 文章标题:Spring Boot 揭秘与实战(四) 配置文件篇 - 有哪些很棒的特性
- 文章链接:http://blog.720ui.com/2016/springboot_04_properties/
Spring Boot 揭秘与实战(四) 配置文件篇 - 有哪些很棒的特性的更多相关文章
- Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 应用监控
文章目录 1. 快速开始 2. 监控和管理端点3. 定制端点 2.1. health 应用健康指标 2.2. info 查看应用信息 2.3. metrics 应用基本指标 2.4. trace 基本 ...
- Spring Boot 揭秘与实战(六) 消息队列篇 - RabbitMQ
文章目录 1. 什么是 RabitMQ 2. Spring Boot 整合 RabbitMQ 3. 实战演练4. 源代码 3.1. 一个简单的实战开始 3.1.1. Configuration 3.1 ...
- Spring Boot 揭秘与实战(三) 日志框架篇 - 如何快速集成日志系统
文章目录 1. 默认的日志框架 logback2. 常用的日志框架 log4j 1.1. 日志级别 1.2. 日志文件 3. 源代码 Java 有很多日志系统,例如,Java Util Logging ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - MyBatis整合
文章目录 1. 环境依赖 2. 数据源3. 脚本初始化 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 4. MyBatis整合5. 总结 4.1. 方案一 通过 ...
- Spring Boot 揭秘与实战(九) 应用监控篇 - 自定义监控端点
文章目录 1. 继承 AbstractEndpoint 抽象类 2. 创建端点配置类 3. 运行 4. 源代码 Spring Boot 提供的端点不能满足我们的业务需求时,我们可以自定义一个端点. 本 ...
- Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控
文章目录 1. 内置 HealthIndicator 监控检测 2. 自定义 HealthIndicator 监控检测 3. 源代码 Health 信息是从 ApplicationContext 中所 ...
- Spring Boot 揭秘与实战(五) 服务器篇 - Tomcat 启用 HTTPS
文章目录 1. 生成证书 2. 配置 HTTPS 支持 3. 启动与测试 4. 源代码 Spring Boot 内嵌的 Tomcat 服务器可以启用 HTTPS 支持. 生成证书 使用第三方 CA 证 ...
- Spring Boot 揭秘与实战(五) 服务器篇 - 其他内嵌服务器 发表于 2017-01-03 | Spring框架 | Spri
文章目录 1. Jetty 的切换 2. Undertow的使用 Spring Boot 可选择内嵌 Tomcat.Jetty 和 Undertow,因此我们不需要以 war 包形式部署项目.< ...
- Spring Boot 揭秘与实战(五) 服务器篇 - Tomcat 代码配置
Spring Boot 内嵌的 Tomcat 服务器默认运行在 8080 端口.如果,我们需要修改Tomcat的端口,我们可以在 src/main/resources/application.prop ...
随机推荐
- 为什么要使用oath协议?
一.如何查看用户是否登录? 通过cookie和session来查看用户是否登录. 如果cookie对应的session中保存了用户登录信息,则判定用户已登录 Jsessionid,也就是tomcat自 ...
- python中的IO模块
1.简介 读写文件是常见的IO操作,python内置了读写文本的函数. 读写文件的模式描述如下: 模式 描述 r 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. rb 以二进制格式打 ...
- 一、Redis数据备份与恢复
Redis里的数据都是保存在内存中,关闭服务器必须进行数据备份. 1.Redis的数据持久化 bgsave做镜像全量持久化,AOF做增量持久化. bgsave的原理:fork和cow(copy on ...
- 基于TcpListerer的web服务器 和 基于HttpListerer的web服务器
摘自<Asp.Net 本质论>作者:郝冠军 /* 为了简化基于TCP协议的监听程序,.NET在System.Net.Sockets命名空间中提供了TcpListerer类,使用它,在构造函 ...
- 【转】别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework
前言 一直以来写的博文都是比较温婉型的博文,今天这篇博文算是一篇批判性博文,有问题欢迎探讨,如标题,你到底会不会用EntityFramework啊. 你到底会不会用EntityFramework啊 面 ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- Windows开启远程桌面服务(Win10)
进入控制面版,找到远程设置 应用确定后Windows服务即会被启动.
- 【转】关于TCP 半连接队列和全连接队列
摘要: # 关于TCP 半连接队列和全连接队列 > 最近碰到一个client端连接异常问题,然后定位分析并查阅各种资料文章,对TCP连接队列有个深入的理解 > > 查资料过程中发现没 ...
- :after 写三角形 border
.tooltip:after { content: ''; position: absolute; border: 6px solid #5190ac; border-color: #5190ac t ...
- 巧用call,appl有 根据对象某一属性求最大值
查找对象数组中某属性的最大最小值的快捷方法 例如要查找array数组中对象的value属性的最大值 var array=[ { "index_id": 119, "are ...