一.配置属性写在系统自带的yml配置文件里面
1.yml配置文件自定义属性
#自定义属性
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注解
@Value("${book.name}")
private String bookName;
@Value("${book.author}")
private String bookAuthor;
1_2.读取方式_@ConfigurationProperties注解
@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文件内容
#注意编码格式 中文建议用unicode
com.book.name = \u4e66\u540d
com.book.author = \u4f5c\u8005
2_1.读取方式
@Component
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "com.book")
public class ConfigBean {
private String name;
private String author;
}