码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-07-27 10:38:37      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:action   1.4   自定义   font   处理   lin   相关   商业   使用   

文章目录

  1. 1. 使用属性文件2. YAML文件
    1. 1.1. 自定义属性
    2. 1.2. 参数引用
    3. 1.3. 随机数属性
    4. 1.4. application-{profile}.properties参数加载
  2. 3. 源代码

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.  
  4. @Value("${author.nickname}")
  5. private String nickname;
  6.  
  7. @Test
  8. public void test1() throws Exception {
  9. System.out.println("real_name : " + realname);
  10. System.out.println("nick_name : " + nickname);
  11. }

参数引用

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

  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.  
  4. @Test
  5. public void test2() throws Exception {
  6. System.out.println("intro : " + intro);
  7. }

随机数属性

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.  
  12. @Test
  13. public void test3() throws Exception {
  14. System.out.println("rand.str : " + randStr);
  15. System.out.println("rand.intid : " + randIntid);
  16. System.out.println("rand.longid : " + randLongid);
  17. System.out.println("rand.number : " + randNumber);
  18. System.out.println("rand.range : " + randRange);
  19. }

application-{profile}.properties参数加载

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

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

  1. #开发环境
  2. application-development.properties
  3.  
  4. #测试环境
  5. application-test.properties
  6.  
  7. #预生产环境
  8. application-preproduction.properties
  9.  
  10. #生产环境
  11. 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.  
  6. @Test
  7. public void test4() throws Exception {
  8. System.out.println("email : " + email);
  9. System.out.println("blog : " + blog);
  10. }

源代码

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

(完)

 

技术分享

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

标签:action   1.4   自定义   font   处理   lin   相关   商业   使用   

原文地址:http://www.cnblogs.com/cnblog-long/p/7243228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!