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. maven聚合工程tomcat插件启动没有 Starting ProtocolHandler ["http-bio-8081"]

    Starting ProtocolHandler ["http-bio-8081"]无法显示,一般有三个原因: (1)数据库连不上: (2)注册中心连不上(我这里用的是zookee ...

  2. 2015-09-29 js2

    Javasript 六. 条件语句 1. 比较运算符 == .!=.>.>=.<.<= 转大/小写:toUpperCase().toLowerCase() 2. 逻辑运算符(与 ...

  3. 【转】Netty之解决TCP粘包拆包(自定义协议)

    1.什么是粘包/拆包 一般所谓的TCP粘包是在一次接收数据不能完全地体现一个完整的消息数据.TCP通讯为何存在粘包呢?主要原因是TCP是以流的方式来处理数据,再加上网络上MTU的往往小于在应用处理的消 ...

  4. 读入excle

    可以输出到csv(逗号间隔,具体搜索csv格式). csv可以在excel中直接导入. 也可以用system函数调用ssconvert从csv转xlsx:system("ssconvert ...

  5. 经典DFS问题实践

    八皇后问题: //八皇后问题 经典的DFS问题实践 #include<iostream> #include<cmath> #include<algorithm> # ...

  6. PHP和Mysql事物处理

    这几天做支付的时候,又用到了事物,为了方便自己以后查看,今天闲的没事就把以前的东西整理下.(其中引用别人的东西,在这里谢谢他们贡献的代码!) 一.事务处理概述: 事务:是若干事件的集合 事务处理:当所 ...

  7. swiftlint 你所要知道的所有!!

    swiftin Should the opening brace of a function or control flow statement be on a new line or not ?:) ...

  8. Android : android 8.0 audio 接口分析

    1.HIDL 的概念 HIDL 读作 hide-l,全称是 Hardware Interface Definition Language.它在 Android Project Treble 中被起草, ...

  9. 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 ...

  10. mvc4使用百度ueditor编辑器

    前言 配置.net mvc4项目使用ueditor编辑器,在配置过程中遇见了好几个问题,以此来记录解决办法.编辑器可以到http://ueditor.baidu.com/website/downloa ...