Spring 框架本身提供了多种的方式来管理配置属性文件。Spring 3.1 之前可以使用 PropertyPlaceholderConfigurer。Spring 3.1 引入了新的环境(Environment)和概要信息(Profile)API,是一种更加灵活的处理不同环境和配置文件的方式。不过 Spring 这些配置管理方式的问题在于选择太多,让开发人员无所适从。Spring Boot 提供了一种统一的方式来管理应用的配置。

使用属性文件

自定义属性

Spring Boot 提供的 SpringApplication 类会搜索并加载 application.properties 文件来获取配置属性值。

创建 application.properties 文件。

  1. author.realname=梁桂钊
  2. author.nickname=LiangGzone

不需要其他配置,我们只需要通过 @Value(“${属性名}”) 注解来加载对应的配置属性,现在,通过单元测试用例来验证吧。

  1. @Value("${author.realname}")
  2. private String realname;
  3. @Value("${author.nickname}")
  4. private String nickname;
  5. @Test
  6. public void test1() throws Exception {
  7. System.out.println("real_name : " + realname);
  8. System.out.println("nick_name : " + nickname);
  9. }

参数引用

此外,我们来可以通过引用参数来使用。

  1. author.product=Spring Boot 揭秘与实战
  2. author.project=springboot-action
  3. author.intro=${author.product} | ${author.project} | 作者:${author.realname}

那么,问题来了, author.intro 通过参数引用得到的结果是什么呢?现在,通过单元测试用例来进行测试。

  1. @Value("${author.intro}")
  2. private String intro;
  3. @Test
  4. public void test2() throws Exception {
  5. System.out.println("intro : " + intro);
  6. }

随机数属性

Spring Boot 的属性配置文件中 ${random} 可以用来生成各种不同类型的随机值,从而简化了代码生成的麻烦,例如 生成 int 值、long 值或者 string 字符串。

  1. # 32位随机字符串
  2. rand.str = ${random.value}
  3. # 随机int类型
  4. rand.intid = ${random.int}
  5. # 随机long类型
  6. rand.longid = ${random.long}
  7. # 100以内的随机int类型
  8. rand.number = ${random.int(100)}
  9. # 0-100范围内的随机int类型
  10. rand.range = ${random.int[0,100]}

附上,单元测试用例。

  1. @Value("${rand.str}")
  2. private String randStr;
  3. @Value("${rand.intid}")
  4. private int randIntid;
  5. @Value("${rand.longid}")
  6. private long randLongid;
  7. @Value("${rand.number}")
  8. private int randNumber;
  9. @Value("${rand.range}")
  10. private String randRange;
  11. @Test
  12. public void test3() throws Exception {
  13. System.out.println("rand.str : " + randStr);
  14. System.out.println("rand.intid : " + randIntid);
  15. System.out.println("rand.longid : " + randLongid);
  16. System.out.println("rand.number : " + randNumber);
  17. System.out.println("rand.range : " + randRange);
  18. }

application-{profile}.properties参数加载

一个非常典型的场景,多环境配置。Spring Boot 也给我们提供了非常简化的配置。

现在,我们根据环境创建4个配置文件。

  1. #开发环境
  2. application-development.properties
  3. #测试环境
  4. application-test.properties
  5. #预生产环境
  6. application-preproduction.properties
  7. #生产环境
  8. application-product.properties

执行命令,通过 active 加载测试环境的配置。

  1. java -jar ***.jar --spring.profiles.active=test

YAML文件

相对于属性文件,YAML 文件是一个更好的配置文件格式。Spring Boot 提供的 SpringApplication 类也提供了对 YAML 配置文件的支持。
创建application.yml 文件

  1. author:
  2. email: lianggzone@163.com
  3. blog: http://blog.720ui.com

那么,我们再来测试一下,是否正常使用哈。

  1. @Value("${author.email}")
  2. private String email;
  3. @Value("${author.blog}")
  4. private String blog;
  5. @Test
  6. public void test4() throws Exception {
  7. System.out.println("email : " + email);
  8. System.out.println("blog : " + blog);
  9. }

源代码

相关示例完整代码: springboot-action

(完)

如果觉得我的文章对你有帮助,请随意打赏。

Spring Boot 揭秘与实战(四) 配置文件篇 - 有哪些很棒的特性的更多相关文章

  1. Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 应用监控

    文章目录 1. 快速开始 2. 监控和管理端点3. 定制端点 2.1. health 应用健康指标 2.2. info 查看应用信息 2.3. metrics 应用基本指标 2.4. trace 基本 ...

  2. Spring Boot 揭秘与实战(六) 消息队列篇 - RabbitMQ

    文章目录 1. 什么是 RabitMQ 2. Spring Boot 整合 RabbitMQ 3. 实战演练4. 源代码 3.1. 一个简单的实战开始 3.1.1. Configuration 3.1 ...

  3. Spring Boot 揭秘与实战(三) 日志框架篇 - 如何快速集成日志系统

    文章目录 1. 默认的日志框架 logback2. 常用的日志框架 log4j 1.1. 日志级别 1.2. 日志文件 3. 源代码 Java 有很多日志系统,例如,Java Util Logging ...

  4. Spring Boot 揭秘与实战(二) 数据存储篇 - MyBatis整合

    文章目录 1. 环境依赖 2. 数据源3. 脚本初始化 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 4. MyBatis整合5. 总结 4.1. 方案一 通过 ...

  5. Spring Boot 揭秘与实战(九) 应用监控篇 - 自定义监控端点

    文章目录 1. 继承 AbstractEndpoint 抽象类 2. 创建端点配置类 3. 运行 4. 源代码 Spring Boot 提供的端点不能满足我们的业务需求时,我们可以自定义一个端点. 本 ...

  6. Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控

    文章目录 1. 内置 HealthIndicator 监控检测 2. 自定义 HealthIndicator 监控检测 3. 源代码 Health 信息是从 ApplicationContext 中所 ...

  7. Spring Boot 揭秘与实战(五) 服务器篇 - Tomcat 启用 HTTPS

    文章目录 1. 生成证书 2. 配置 HTTPS 支持 3. 启动与测试 4. 源代码 Spring Boot 内嵌的 Tomcat 服务器可以启用 HTTPS 支持. 生成证书 使用第三方 CA 证 ...

  8. Spring Boot 揭秘与实战(五) 服务器篇 - 其他内嵌服务器 发表于 2017-01-03 | Spring框架 | Spri

    文章目录 1. Jetty 的切换 2. Undertow的使用 Spring Boot 可选择内嵌 Tomcat.Jetty 和 Undertow,因此我们不需要以 war 包形式部署项目.< ...

  9. Spring Boot 揭秘与实战(五) 服务器篇 - Tomcat 代码配置

    Spring Boot 内嵌的 Tomcat 服务器默认运行在 8080 端口.如果,我们需要修改Tomcat的端口,我们可以在 src/main/resources/application.prop ...

随机推荐

  1. 为什么要使用oath协议?

    一.如何查看用户是否登录? 通过cookie和session来查看用户是否登录. 如果cookie对应的session中保存了用户登录信息,则判定用户已登录 Jsessionid,也就是tomcat自 ...

  2. python中的IO模块

    1.简介 读写文件是常见的IO操作,python内置了读写文本的函数. 读写文件的模式描述如下: 模式 描述 r 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. rb 以二进制格式打 ...

  3. 一、Redis数据备份与恢复

    Redis里的数据都是保存在内存中,关闭服务器必须进行数据备份. 1.Redis的数据持久化 bgsave做镜像全量持久化,AOF做增量持久化. bgsave的原理:fork和cow(copy on  ...

  4. 基于TcpListerer的web服务器 和 基于HttpListerer的web服务器

    摘自<Asp.Net 本质论>作者:郝冠军 /* 为了简化基于TCP协议的监听程序,.NET在System.Net.Sockets命名空间中提供了TcpListerer类,使用它,在构造函 ...

  5. 【转】别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework

    前言 一直以来写的博文都是比较温婉型的博文,今天这篇博文算是一篇批判性博文,有问题欢迎探讨,如标题,你到底会不会用EntityFramework啊. 你到底会不会用EntityFramework啊 面 ...

  6. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  7. Windows开启远程桌面服务(Win10)

    进入控制面版,找到远程设置 应用确定后Windows服务即会被启动.

  8. 【转】关于TCP 半连接队列和全连接队列

    摘要: # 关于TCP 半连接队列和全连接队列 > 最近碰到一个client端连接异常问题,然后定位分析并查阅各种资料文章,对TCP连接队列有个深入的理解 > > 查资料过程中发现没 ...

  9. :after 写三角形 border

    .tooltip:after { content: ''; position: absolute; border: 6px solid #5190ac; border-color: #5190ac t ...

  10. 巧用call,appl有 根据对象某一属性求最大值

    查找对象数组中某属性的最大最小值的快捷方法 例如要查找array数组中对象的value属性的最大值 var array=[ { "index_id": 119, "are ...