Springboot配置文件
Springboot配置文件
Properties
。。。
yaml
YAML(/ˈjæməl/,尾音类似camel骆驼)是一个可读性高,用来表达数据序列化的格式。YAML参考了其他多种语言,包括:C语言、Python、Perl,并从XML、电子邮件的数据格式(RFC 2822)中获得灵感。Clark Evans在2001年首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者。当前已经有数种编程语言或脚本语言支持(或者说解析)这种语言。
YAML是"YAML Ain't a Markup Language"(YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名
@ConfigurationProperties(prefix = "配置文件")
实体类与配置文件绑定
@Data
@Component
@ConfigurationProperties(prefix = "makaeluser")
public class User {
private String name;
private Integer age;
private Date birthday;
}
makaeluser:
name: Makael
age: 22
birthday: 1999/06/11
@SpringBootTest
class BackstageApplicationTests {
@Autowired
User user;
@Test
void contextLoads() { //run test
System.out.println(user);
}
}

好处:

JSR303校验
导入启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
User.class
package com.immortal.backstage.pojo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
@Data
@Component
@ConfigurationProperties(prefix = "admin")
@Validated //校验
public class User {
private String name;
private Integer age;
private Date birthday;
@Email //邮箱格式
private String Email;
}
yaml
admin:
name: Makael
age: 22
birthday: 1999/06/11
Email: 17610373883 //这里更改成邮箱格式


Springboot配置
Springboot所有的配置类上面都标识了@ConfigurationProperties(prefix = "")

浙公网安备 33010602011771号