武漢單位做網(wǎng)站網(wǎng)站建設(shè)優(yōu)化400報(bào)價(jià)
SpringBoot 配置文件
properties配置文件
application.properties
以配置端口和訪問路徑為例
server.port=8080
yaml配置文件
application.yml / application.yaml
server:port: 81
在實(shí)際開發(fā)中,更常用的是yaml配置文件
yaml層級表示更加明顯
yml配置信息書寫與獲取
lesson: SpringBootenterprise:name: itcatage: 16tel: 4000161933subject:- java- 前端- 大數(shù)據(jù)
yml書寫注意事項(xiàng):
? ? ? ? 值前邊必須有空格,作為分隔符
? ? ? ? 使用空格作為縮進(jìn)表示層級關(guān)系,相同的層級左對齊
獲取
//yaml文件數(shù)據(jù)讀取1@Value("${lesson}")private String lesson;@Value("${enterprise.subject[0]}")private String subject_00;//yaml文件數(shù)據(jù)讀取2@Autowiredprivate Environment environment;//yaml文件數(shù)據(jù)讀取3@Autowiredprivate Enterprise enterprise;
方式三實(shí)體類如下
//封裝yaml對象格式數(shù)據(jù)必須先聲明當(dāng)前實(shí)體類受Spring管控
@Component
//使用@ConfigurationProperties注解定義當(dāng)前實(shí)體類讀取配置屬性信息,通過prefix屬性設(shè)置讀取哪個(gè)數(shù)據(jù)
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {private String name;private Integer age;private String tel;private String[] subject;