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 ...
随机推荐
- oracle数据库备份任务
备份脚本如下: 1.0 expdp1.1导出某些schema #!/bin/bash ORACLE_BASE=/oracle/productexport ORACLE_BASEORACLE_HOME= ...
- Homebrew 备忘
每次都搜,写篇博客记录以备后续查看. 安装 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew ...
- Angular4.x 引入第三方 JS
引入 Jquery 使用 angular-cli 新建 angular项目 1.安装 jquery npm install jquery --save 2..angular-cli.json 中引入 ...
- std::string 的方法c_str() 和 data() 有什么区别
1.从C++标准上的解释来看,只有一点区别: c_str() 返回一个指向正规C字符串的指针常量,该指针保证指向一个 size() + 1 长度的空间,而且最后一个字符肯定是 \0 : 而 data( ...
- Win10系列:VC++ XML文件解析
XML文件按照元素标记来存储数据,通过遍历这些元素标记可以得到XML文件中所保存的数据.在C++/CX的类库中并未定义用于解析XML文件的类,但C++提供了能解析XML文件的框架和类库,如msxml4 ...
- Introduction to Cryto & Crptocurrencies Lecture 1
Lecture 1.2 Hash Pointer & Data Structure Use Case 1. 什么是Block Chain呢? 想象一个像链表一样的结构,只不过与通常的指向下一块 ...
- http协商缓存VS强缓存
之前一直对浏览器缓存只能描述一个大概,深层次的原理不能描述上来:终于在前端的两次面试过程中被问倒下,为了泄恨,查阅一些资料最终对其有了一个更深入的理解,废话不多说,赶紧来看看浏览器缓存的那些事吧,有不 ...
- sqlalchemy(二)简单的连接示例
# -*- coding: utf-8 -*- import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.d ...
- 从mysql读取数据写入mongo
# coding:utf-8 # Created by qinlin.liu at 2017/3/14 import pymysql import datetime #pymongo说明文档 : h ...
- border_mode
如果border_mode选择为same,那么卷积操作的输入和输出尺寸会保持一致.如果选择valid,那卷积过后,尺寸会变小 # apply a 3x3 convolution with 64 out ...