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 ...
随机推荐
- maven聚合工程tomcat插件启动没有 Starting ProtocolHandler ["http-bio-8081"]
Starting ProtocolHandler ["http-bio-8081"]无法显示,一般有三个原因: (1)数据库连不上: (2)注册中心连不上(我这里用的是zookee ...
- 2015-09-29 js2
Javasript 六. 条件语句 1. 比较运算符 == .!=.>.>=.<.<= 转大/小写:toUpperCase().toLowerCase() 2. 逻辑运算符(与 ...
- 【转】Netty之解决TCP粘包拆包(自定义协议)
1.什么是粘包/拆包 一般所谓的TCP粘包是在一次接收数据不能完全地体现一个完整的消息数据.TCP通讯为何存在粘包呢?主要原因是TCP是以流的方式来处理数据,再加上网络上MTU的往往小于在应用处理的消 ...
- 读入excle
可以输出到csv(逗号间隔,具体搜索csv格式). csv可以在excel中直接导入. 也可以用system函数调用ssconvert从csv转xlsx:system("ssconvert ...
- 经典DFS问题实践
八皇后问题: //八皇后问题 经典的DFS问题实践 #include<iostream> #include<cmath> #include<algorithm> # ...
- PHP和Mysql事物处理
这几天做支付的时候,又用到了事物,为了方便自己以后查看,今天闲的没事就把以前的东西整理下.(其中引用别人的东西,在这里谢谢他们贡献的代码!) 一.事务处理概述: 事务:是若干事件的集合 事务处理:当所 ...
- swiftlint 你所要知道的所有!!
swiftin Should the opening brace of a function or control flow statement be on a new line or not ?:) ...
- Android : android 8.0 audio 接口分析
1.HIDL 的概念 HIDL 读作 hide-l,全称是 Hardware Interface Definition Language.它在 Android Project Treble 中被起草, ...
- Codeforces Round #506 (Div. 3) D. Concatenated Multiples
D. Concatenated Multiples You are given an array aa, consisting of nn positive integers. Let's call ...
- mvc4使用百度ueditor编辑器
前言 配置.net mvc4项目使用ueditor编辑器,在配置过程中遇见了好几个问题,以此来记录解决办法.编辑器可以到http://ueditor.baidu.com/website/downloa ...