1.随机数
比如
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
2.占位符获取之前配置的值,如果没有可以是用:指定默认值
person.hello:hello如果person.hello没有指定值则默认为hello,如果在配置文件中指定了person.hello=test则person.dog.name= p e r s o n . h e l l o : h e l l o 如 果 p e r s o n . h e l l o 没 有 指 定 值 则 默 认 为 h e l l o , 如 果 在 配 置 文 件 中 指 定 了 p e r s o n . h e l l o = t e s t 则 p e r s o n . d o g . n a m e = {person.hello:hello}_dog在运行时候打印为test_dog
person.last‐name=张三${random.uuid}
person.age=${random.int}
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=${person.hello:hello}_dog
person.dog.age=15
3.运行
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
application.properties
person.last-name=张三${random.uuid}
person.age=${random.int}
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=${person.hello:hello}_dog
person.dog.age=15
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootConfigApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
运行结果:
Person{lastName='张三10ee71ca-8ecb-4fab-a4b5-422b54e16d3d', age=-871503876, boss=false, birth=Fri Dec 15 00:00:00 CST 2017, maps={k1=v1, k2=14}, lists=[a, b, c], dog=Dog{name='hello_dog', age=15}}