properties随机数与配置文件占位符
生活随笔
收集整理的這篇文章主要介紹了
properties随机数与配置文件占位符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
理論
例子
理論
RandomValuePropertySource:配置文件中可以使用隨機數
${random.value}、${random.int}、${random.long}、${random.int(10)}、${random.int[1024, 65534]}
?
屬性配置占位符
people.name="你妹" people.alias=${people.name}"呵呵"?
例子
程序結構如下:
運行截圖如下:
源碼如下:
People.java
package com.setactive.demo.bean;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;@Component @ConfigurationProperties(prefix = "people") public class People {private String name;private String aliay;private int age;private int weight;private int height;@Overridepublic String toString() {return "People{" +"name='" + name + '\'' +", aliay='" + aliay + '\'' +", age=" + age +", weight=" + weight +", height=" + height +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAliay() {return aliay;}public void setAliay(String aliay) {this.aliay = aliay;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getWeight() {return weight;}public void setWeight(int weight) {this.weight = weight;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;} }DemoApplication.java
package com.setactive.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}application.properties
people.name="你妹"${random.uuid} people.aliay="日了狗了"${random.uuid} people.height=${random.int} people.weight=${random.int}DemoApplicationTests.java
package com.setactive.demo;import com.setactive.demo.bean.People; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests {@AutowiredPeople people;@Testpublic void contextLoads() {System.out.println(people);}}修改application.properties文件
#people.name="你妹"${random.uuid} people.aliay=${people.name:呵呵}呵呵噠 people.height=${random.int} people.weight=${random.int}運行截圖如下:
總結
以上是生活随笔為你收集整理的properties随机数与配置文件占位符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 系统架构师学习笔记-多媒体基础知识
- 下一篇: SQL文档阅读笔记-对水平分区和垂直分区