关于springboot:springboot开发笔记6属性占位符以及默认值

28次阅读

共计 928 个字符,预计需要花费 3 分钟才能阅读完成。

上一节:SpringBoot 开发笔记 -(5)- 导入内部资源: @ImportResource


6.1 随机数

${random.value}

${random.int}

${random.long}

${random.uuid} : 随机 uuid 字符串;

${random.int(10)} : 从 0 -10 中的一个随机数;

${random.int[10,20]} : 从 10 到 20 两头的一个随机数, 注: 通过测试, 10 和 20 两头还不能有空格, 否则有效!

6.2 如果没有, 指定默认值:

${person.lastName: 无名}

person:
  lastName: ${random.uuid}
  age: ${random.int(100)}
  male: true
  birth: 1985/03/03
  maps: {k1: v1, k2: 20, k3: true}
  lists:
    - lisi
    - wangwu
  dog:
    name: ${person.lastName: 无名}_小黄
    age: ${random.int[10,20]}
{"lastName":"dffc65aa-8d1b-4110-ada5-debcf59d199b","age":98,"male":true,"birth":"Mar 3, 1985 12:00:00 AM","maps":{"k1":"v1","k2":20,"k3":true},"lists":["lisi","wangwu"],"dog":{"name":"34c4bf97-2885-4a61-b0e6-f9fb176fc86f_小黄","age":11}}

如果把 lastName->firstName, 找不到此字段, 就会应用默认值: “ 无名 ”

  dog:
    name: ${person.firstName: 无名}_小黄 
{"lastName":"6ea9265a-a0a2-45cf-bdde-95fbd71305cf","age":39,"male":true,"birth":"Mar 3, 1985 12:00:00 AM","maps":{"k1":"v1","k2":20,"k3":true},"lists":["lisi","wangwu"],"dog":{"name":"无名_小黄","age":17}}

正文完
 0