一、配置文件java
SpringBoot使用一個全局的配置文件,配置文件名是固定的;web
•application.propertiesspring
•application.ymlapache
配置文件的做用:修改SpringBoot自動配置的默認值;SpringBoot在底層都給咱們自動配置好;json
YAML(YAML Ain’t Markup Language)數組
YAML A Markup Language:是一個標記語言 YAML isn't Markup Language:不是一個標記語言;
標記語言:springboot
之前的配置文件;大多都使用的是 xxxx.xml文件; YAML:以數據爲中心,比json、xml等更適合作配置文件; YAML:配置例子 server: port: 8081 XML: <server> <port>8081</port> </server>
兩種配置效果是同樣的markdown
一、基本語法app
k:(空格)v:表示一對鍵值對(空格必須有);maven
以空格的縮進來控制層級關係;只要是左對齊的一列數據,都是同一個層級的
server: port: 8081 path: /hello
屬性和值也是大小寫敏感;
二、值的寫法
字面量:普通的值(數字,字符串,布爾)
k: v:字面直接來寫; 字符串默認不用加上單引號或者雙引號; "":雙引號;不會轉義字符串裏面的特殊字符;特殊字符會做爲自己想表示的意思 name: "zhangsan \n lisi":輸出;zhangsan 換行 lisi '':單引號;會轉義特殊字符,特殊字符最終只是一個普通的字符串數據 name: ‘zhangsan \n lisi’:輸出;zhangsan \n lisi
對象、Map(屬性和值)(鍵值對):
k: v:在下一行來寫對象的屬性和值的關係;注意縮進 對象仍是k: v的方式 friends: lastName: zhangsan age: 20
行內寫法:
friends: {lastName: zhangsan,age: 18}
數組(List、Set):
用- 值表示數組中的一個元素
pets: - cat - dog - pig
行內寫法
pets: [cat,dog,pig]
注意:
Idea提供get、set方法快捷鍵:Alt+Insert,也能夠右擊選擇調出
上面的格式對應着下面的值
三、配置文件值注入
配置文件
person: lastName: hello age: 18 boss: false birth: 2017/12/12 maps: {k1: v1,k2: 12} lists: - lisi - zhaoliu dog: name: 小狗 age: 12
javaBean:
/** * 將配置文件中配置的每個屬性的值,映射到這個組件中 * @ConfigurationProperties:告訴SpringBoot將本類中的全部屬性和配置文件中相關的配置進行綁定; * prefix = "person":配置文件中哪一個下面的全部屬性進行一一映射 * * 只有這個組件是容器中的組件,才能容器提供的@ConfigurationProperties功能; * */ @Component @ConfigurationProperties(prefix = "person") public class Person { private String lastName; private Integer age; private Boolean boss; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog;
咱們能夠導入配置文件處理器,之後編寫配置就有提示了
<!--導入配置文件處理器,配置文件進行綁定就會有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
案例:
Person.java
package com.xdr.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; /* 將配置文件中配置的每個屬性的值映射到這個組件中 ConfigurationProperties:告訴springboot將本類中的全部屬性和配置文件中的相關配置進行綁定 prefix="person":配置文件中哪一個下面的屬性進行一一映射 只有這個組件是容器中的組件,才能是容器@ConfigurationProperties提供功能 */ @Component @ConfigurationProperties (prefix = "person") public class Person { private String lastName; private Integer age; private Boolean boss; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getBoss() { return boss; } public void setBoss(Boolean boss) { this.boss = boss; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public List<Object> getLists() { return lists; } public void setLists(List<Object> lists) { this.lists = lists; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } @Override public String toString() { return "Person{" + "lastName='" + lastName + '\'' + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}'; } }
Dog.java
package com.xdr.bean; public class Dog { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
在pom.xml添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xdr</groupId> <artifactId>02_springboot_config</artifactId> <version>0.0.1-SNAPSHOT</version> <name>02_springboot_config</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--導入配置文件處理器,配置文件進行綁定就會有提示 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
從新運行主程序看是否還有剛纔的錯誤
打開單元測試,測試上面配置的值是否打印的出來
ApplicationTests.java
package com.xdr; import com.xdr.bean.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * springboot單元測試 * 能夠在測試期間很方便的相似編碼同樣進行自動注入等容器的功能 */ @RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Autowired Person person; @Test public void contextLoads() { System.out.println(person); } }