欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

properties随机数与配置文件占位符

发布时间:2025/3/15 编程问答 28 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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随机数与配置文件占位符的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。