Springboot 配置文件占位符

1、配置文件 resources/config/application.properties

// 随机id, ID:拼接随机的uuid字符串
product.id=ID:${random.uuid}
product.name=da mao mao
// 随机的int类型数据
product.weight=${random.int}
// 500到600之间的随机数,左右不包含
product.firstLinePrice=${random.int(500,600)}
// 300到400之间的随机数,左右都包含
product.endLinePrice=${random.int[300,400]}
// 获取上面的product.name的值
product.remark=${product.name}

2、测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot01ApplicationTests {
    @Value("${product.id}")
    private String id;

    @Value("${product.name}")
    private String name;

    @Value("${product.weight}")
    private Integer weight;

    @Value("${product.firstLinePrice}")
    private Integer firstLinePrice;

    @Value("${product.endLinePrice}")
    private Integer endLinePrice;

    @Value("${product.remark}")
    private String remark;

    @Test
    public void testProperties() {
        System.out.println(id + "\r\n" + name + "\r\n" + weight + "\r\n" + 
		firstLinePrice + "\r\n" + endLinePrice + "\r\n" + remark);
    }
}

3、测试结果

ID:f0158ded-844f-456e-879b-efa36f4c593f
da mao mao
1877884196
506
315
da mao mao

  

 

posted @ 2019-12-24 14:54  变体精灵  阅读(722)  评论(0编辑  收藏  举报