第二天:SpringBoot之Application.properties
Application.properties主要用来记录一些参数配置如数据库。
1· 新建User.java文件,包名:com.example.demo.entity;
添加必要的注解,这里用全类名+属性名作为value,然后添加setter跟getter方法.
2· 打开application.properties文件
这里的命名跟User中的Value保持一致
3. 读取配置的属性
这里用自带的测试类,并且@AutoWeird会在运行过程中构造对象
4. 补充:可能会存在一种情况需要多种配置
这里新建了几个.properties,并修改了application.properties,添加了一个属性,指定application-normal作为项目使用的properties文件.
其余两个properties里面属性一样替换了属性值,下面测试一下:
另外发布的时候可以通过命令行指定使用哪个properties文件:
java -jar xxx.war --spring.profiles.active=demo
5. 关于properties参数值的一些细节:
# 随机字符串
com.example.demo.entity.User.id=${random.value}
# 随机int
com.example.demo.entity.User.id=${random.int}
# 随机long
com.example.demo.entity.User.id=${random.long}
# 10以内的随机数
com.example.demo.entity.User.id=${random.int(10)}
# 10-20的随机数