1、配置文件中不能用username作为前缀
这样会以定义的@Author 的值为username**
2、properties文件乱解决
spring.banner.charset=UTF-8
server.tomcat.uri-encoding=UTF-8
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
spring.messages.encoding=UTF-8
1、放在文件开头
2、配置idea编码格式
3、静态属性不能通过直接读取配置文件赋值
4、配置文件中能获得随机值(uuid等)
# 随机数
# 随机int
test.randomInt=${random.int}
# 随机10以内
test.randomIntMax=${random.int(10)}
# 随机20-50
test.randomIntMiddle=${random.int(20,50)}
# 随机Long
test.randomLong=${random.long}
# 字符串
test.randomValue=${random.value}
# uuid
test.randomUuid=${random.uuid}
# key不能random开头,使用时会有问题
#random.num=${random.int}
5、通过读取配置文件给对象注入值
stu.id=1
stu.name=轻松大大
@Component
@PropertySource({"classpath:application.properties"})
@ConfigurationProperties(prefix = "stu")
@Data
public class StudentProperties {
private int id;
private String name;
}
步骤1、读取包含注入值的配置文件
@PropertySource({"classpath:application.properties"})
步骤2、读取前缀
@ConfigurationProperties(prefix = "stu")
步骤三、将对象交给springboot管理
@Component
步骤四、给予属性的名称必须和前缀后的名称一直
测试: