目录
理论
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;
@Override
public 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 {
@Autowired
People people;
@Test
public void contextLoads() {
System.out.println(people);
}
}
修改application.properties文件
#people.name="你妹"${random.uuid}
people.aliay=${people.name:呵呵}呵呵哒
people.height=${random.int}
people.weight=${random.int}
运行截图如下: