一.配置属性写在系统自带的yml配置文件里面
1.yml配置文件自定义属性
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#自定义属性 book: name: name author: author #随机字符串 value: ${random.value} #随机int值 intValue: ${random.int} #随机long值 longValue: ${random.long} #随机uuid uuid: ${random.uuid} #1000以内随机数 randomNumber: ${random.int(1000)} #10-100以内随机数 randomNum: ${random.int[10,100]} #自定义属性之间的引用 title: 书名是:${book.name}
1_1.读取方式_@value注解
复制代码
1
2
3
4
5@Value("${book.name}") private String bookName; @Value("${book.author}") private String bookAuthor;
1_2.读取方式_@ConfigurationProperties注解
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19@ConfigurationProperties(prefix = "book") public class BookConfigBean { private String name; private String author; //随机字符串 private String value; //随机int值 private int intValue; //随机long值 private long longValue; //随机uuid private String uuid; //1000以内随机数 private int randomNumber; //10-100以内随机数 private int randomNum; //自定义属性之间的引用 private String title; }
二.自定义配置文件test.properties
2.test.properties文件内容
复制代码
1
2
3#注意编码格式 中文建议用unicode com.book.name = u4e66u540d com.book.author = u4f5cu8005
2_1.读取方式
复制代码
1
2
3
4
5
6
7
8@Component @PropertySource(value = "classpath:test.properties") @ConfigurationProperties(prefix = "com.book") public class ConfigBean { private String name; private String author; }
最后
以上就是美好酒窝最近收集整理的关于springboot-读取配置文件属性二.自定义配置文件test.properties的全部内容,更多相关springboot-读取配置文件属性二内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复