• Spring Boot配置文件详解:自定义属性、随机数、多环境配置


    自定义属性与加载

    我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义:

    application-dev.yml

      1 com.didispace.blog:
      2 
      3 name: 程序猿DD
      4 
      5 title: Spring Boot教程
      6 
      7 desc: ${com.didispace.blog.name}正在努力写《${com.didispace.blog.title}》
      8 
      9 # 随机字符串
     10 
     11 value: ${random.value}
     12 
     13 # 随机int
     14 
     15 number: ${random.int}
     16 
     17 # 随机long
     18 
     19 bignumber: ${random.long}
     20 
     21 # 10以内的随机数
     22 
     23 test1: ${random.int(10)}
     24 
     25 # 10-20的随机数
     26 
     27 test2: ${random.int[10,20]}
     28 
      1 package com.wls.integrateplugs.property;
      2 
      3 import org.springframework.beans.factory.annotation.Value;
      4 
      5 import org.springframework.stereotype.Component;
      6 
      7 /**
      8 
      9 * @author 程序猿DD
     10 
     11 * @version 1.0.0
     12 
     13 * @date 16/5/5 下午12:16.
     14 
     15 * @blog http://blog.didispace.com
     16 
     17 */
     18 
     19 @Component
     20 
     21 public class BlogProperties {
     22 
     23 @Value("${com.didispace.blog.name}")
     24 
     25 private String name;
     26 
     27 @Value("${com.didispace.blog.title}")
     28 
     29 private String title;
     30 
     31 @Value("${com.didispace.blog.desc}")
     32 
     33 private String desc;
     34 
     35 @Value("${com.didispace.blog.value}")
     36 
     37 private String value;
     38 
     39 @Value("${com.didispace.blog.number}")
     40 
     41 private Integer number;
     42 
     43 @Value("${com.didispace.blog.bignumber}")
     44 
     45 private Long bignumber;
     46 
     47 @Value("${com.didispace.blog.test1}")
     48 
     49 private Integer test1;
     50 
     51 @Value("${com.didispace.blog.test2}")
     52 
     53 private Integer test2;
     54 
     55 public String getName() {
     56 
     57 return name;
     58 
     59 }
     60 
     61 public void setName(String name) {
     62 
     63 this.name = name;
     64 
     65 }
     66 
     67 public String getTitle() {
     68 
     69 return title;
     70 
     71 }
     72 
     73 public void setTitle(String title) {
     74 
     75 this.title = title;
     76 
     77 }
     78 
     79 public String getDesc() {
     80 
     81 return desc;
     82 
     83 }
     84 
     85 public void setDesc(String desc) {
     86 
     87 this.desc = desc;
     88 
     89 }
     90 
     91 public String getValue() {
     92 
     93 return value;
     94 
     95 }
     96 
     97 public void setValue(String value) {
     98 
     99 this.value = value;
    100 
    101 }
    102 
    103 public Integer getNumber() {
    104 
    105 return number;
    106 
    107 }
    108 
    109 public void setNumber(Integer number) {
    110 
    111 this.number = number;
    112 
    113 }
    114 
    115 public Long getBignumber() {
    116 
    117 return bignumber;
    118 
    119 }
    120 
    121 public void setBignumber(Long bignumber) {
    122 
    123 this.bignumber = bignumber;
    124 
    125 }
    126 
    127 public Integer getTest1() {
    128 
    129 return test1;
    130 
    131 }
    132 
    133 public void setTest1(Integer test1) {
    134 
    135 this.test1 = test1;
    136 
    137 }
    138 
    139 public Integer getTest2() {
    140 
    141 return test2;
    142 
    143 }
    144 
    145 public void setTest2(Integer test2) {
    146 
    147 this.test2 = test2;
    148 
    149 }
    150 
    151 }
    152 

    按照惯例,通过单元测试来验证BlogProperties中的属性是否已经根据配置文件加载了。然后通过@Value("${属性名}")注解来加载对应的配置属性,具体如下:

    单元测试

      1 package com.wls.test.integrateplugs.property;
      2 
      3 import com.wls.integrateplugs.property.BlogProperties;
      4 
      5 import org.apache.commons.logging.Log;
      6 
      7 import org.apache.commons.logging.LogFactory;
      8 
      9 import org.junit.Assert;
     10 
     11 import org.junit.Test;
     12 
     13 import org.junit.runner.RunWith;
     14 
     15 import org.springframework.beans.factory.annotation.Autowired;
     16 
     17 import org.springframework.boot.test.context.SpringBootTest;
     18 
     19 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     20 
     21 /**
     22 
     23 *
     24 
     25 * @author 程序猿DD
     26 
     27 * @version 1.0.0
     28 
     29 * @blog http://blog.didispace.com
     30 
     31 *
     32 
     33 */
     34 
     35 @RunWith(SpringJUnit4ClassRunner.class)
     36 
     37 @SpringBootTest
     38 
     39 public class BlogPropertiesTest {
     40 
     41 private static final Log log = LogFactory.getLog(BlogPropertiesTest.class);
     42 
     43 @Autowired
     44 
     45 private BlogProperties blogProperties;
     46 
     47 @Test
     48 
     49 public void test1() throws Exception {
     50 
     51 Assert.assertEquals("程序猿DD", blogProperties.getName());
     52 
     53 Assert.assertEquals("Spring Boot教程", blogProperties.getTitle());
     54 
     55 Assert.assertEquals("程序猿DD正在努力写《Spring Boot教程》", blogProperties.getDesc());
     56 
     57 log.info("随机数测试输出:");
     58 
     59 log.info("随机字符串 : " + blogProperties.getValue());
     60 
     61 log.info("随机int : " + blogProperties.getNumber());
     62 
     63 log.info("随机long : " + blogProperties.getBignumber());
     64 
     65 log.info("随机10以下 : " + blogProperties.getTest1());
     66 
     67 log.info("随机10-20 : " + blogProperties.getTest2());
     68 
     69 }
     70 
     71 }
     72 

    参数间的引用

    application.properties中的各个参数之间也可以直接引用来使用,就像下面的设置:

      1 com.didispace.blog.name=程序猿DD
      2 
      3 com.didispace.blog.title=Spring Boot教程
      4 
      5 com.didispace.blog.desc=${com.didispace.blog.name}正在努力写《${com.didispace.blog.title}》
      6 
      7 com.didispace.blog.desc参数引用了上文中定义的name和title属性,最后该属性的值就是程序猿DD正在努力写《Spring Boot教程》。
      8 

    使用随机数

    在一些情况下,有些参数我们需要希望它不是一个固定的值,比如密钥、服务端口等。Spring Boot的属性配置文件中可以通过${random}来产生int值、long值或者string字符串,来支持属性的随机值。

      1 # 随机字符串
      2 
      3 com.didispace.blog.value=${random.value}
      4 
      5 # 随机int
      6 
      7 com.didispace.blog.number=${random.int}
      8 
      9 # 随机long
     10 
     11 com.didispace.blog.bignumber=${random.long}
     12 
     13 # 10以内的随机数
     14 
     15 com.didispace.blog.test1=${random.int(10)}
     16 
     17 # 10-20的随机数
     18 
     19 com.didispace.blog.test2=${random.int[10,20]}
     20 

    通过命令行设置属性值

    相信使用过一段时间Spring Boot的用户,一定知道这条命令:java -jar xxx.jar --server.port=8888,通过使用–server.port属性来设置xxx.jar应用的端口为8888。

    在命令行运行时,连续的两个减号--就是对application.properties中的属性值进行赋值的标识。所以,java -jar xxx.jar --server.port=8888命令,等价于我们在application.properties中添加属性server.port=8888,该设置在样例工程中可见,读者可通过删除该值或使用命令行来设置该值来验证。

    通过命令行来修改属性值固然提供了不错的便利性,但是通过命令行就能更改应用运行的参数,那岂不是很不安全?是的,所以Spring Boot也贴心的提供了屏蔽命令行访问属性的设置,只需要这句设置就能屏蔽:SpringApplication.setAddCommandLineProperties(false)

    多环境配置

    我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。

    对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,Spring Boot也不例外,或者说更加简单。

    在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

    • application-dev.properties:开发环境
    • application-test.properties:测试环境
    • application-prod.properties:生产环境

    至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

    如:spring.profiles.active=test就会加载application-test.properties配置文件内容

    下面,以不同环境配置不同的服务端口为例,进行样例实验。

    • 针对各环境新建不同的配置文件application-dev.propertiesapplication-test.propertiesapplication-prod.properties

    • 在这三个文件均都设置不同的server.port属性,如:dev环境设置为1111,test环境设置为2222,prod环境设置为3333

    • application.properties中设置spring.profiles.active=dev,就是说默认以dev环境设置

    • 测试不同配置的加载

      • 执行java -jar xxx.jar,可以观察到服务端口被设置为1111,也就是默认的开发环境(dev)
      • 执行java -jar xxx.jar --spring.profiles.active=test,可以观察到服务端口被设置为2222,也就是测试环境的配置(test)
      • 执行java -jar xxx.jar --spring.profiles.active=prod,可以观察到服务端口被设置为3333,也就是生产环境的配置(prod)

    按照上面的实验,可以如下总结多环境的配置思路:

    • application.properties中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认配置
    • application-{profile}.properties中配置各个环境不同的内容
    • 通过命令行方式去激活不同环境的配置

    引用:air_balloon

  • 相关阅读:
    编程用外星人宏建设置教程(在网上找了好久没几个相关帖子,自己研究写下来留个备忘吧)
    通过selenium+pyautogui模拟登陆淘宝(完美实现)
    python之字符串的五种拼接方式
    python之批量文件重命名
    爬虫系列之链家的信息爬取及数据分析
    跟潭州学院的强子老师学习网络爬虫---爬取全书网
    Python之编写测试用例,unittest模块中的TestCase类中的六种断言方法,以及setUp()函数。
    Python学习之路
    GridBagLayout的帮助类
    eclipse和cygwin搭建C++环境的修正版本
  • 原文地址:https://www.cnblogs.com/ldy-blogs/p/8621830.html
Copyright © 2020-2023  润新知