1application.yml的配置及其语法
一般是冒号后加一个空格:
server:
port: 8081
map的写法:
friends: {lastName: zhangsan,age: 18}
配置文件值注入给JavaBean示例如下:
myuser类:
@Component
//一定要先被放进spring工厂,才能用以下配置
@ConfigurationProperties(prefix = "myuser")
//读取配置文件,进行映射,具体可见我的上一篇文章
public class myuser {
public String name;
public String password;
public String id;
}
application.yml:
myuser:
username: lisi
password: 12456
id: 222222
二配置文件的加载位置
-file:./config/
-file:./
-classpath:/config/
-classpath:/
即根目录下的config目录下,然后是 根目录下,然后是classpath路径下的config目录下,最后是classpath路径下。
优先级由高到低,高优先级的配置会覆盖低优先级的配置。
三springboot中使用@Validated注解对数据进行校验
1先在类开头加上@Validated,再在属性或参数上加以下注解即可: