applicationi.properties
application.yml 以數據為中心,比json xml更適合做配置文件
k: v 表示一對鍵值對 空格縮進控制層級關系,左對齊的一列數據都是同一層級
字面量:123 aaa字符串默認不用引號 true
""不轉義,所以特殊字符比如換行生效
''轉義,所以特殊字符當成普通字符無效
對象,map屬性值
對象還是k: v
在下一行寫
friends:
lastname: zhangsan
age: 20
行內寫法:
friends: {lastname: zhangsan,age: 18}
數組List、Set, pets: - cat - dog - pig 行內寫法: pets: [cat,dog,pig]
干活:
創建bean目錄寫一個person類,一個dog類
@ConfigurationProperties將配置文件中配置的每一個屬性的值,映射到這個組件中
package com.tabctrlshift.springboot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @ConfigurationProperties將配置文件中配置的每一個屬性的值,映射到這個組件中
* prefix = "person"配置文件中這個下面的所有屬性進行一一映射
*/
//類中所有屬性和配置文件中相關的配置進行綁定
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private boolean boos;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boos=" + boos +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", 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 isBoos() {
return boos;
}
public void setBoos(boolean boos) {
this.boos = boos;
}
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;
}
}
package com.tabctrlshift.springboot.bean;
public class Dog {
private String name;
private String age;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
application.yml寫入配置
server:
port: 8081
person:
lastName: zhangsan
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
- list
- zhaoliu
dog:
name: 小狗
age: 2

錯誤提示:
添加相關的依賴就有元數據提示了


<!--導入配置文件處理器,配置文件綁定就會有輸入提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
配置好了重新啟動main

@Component 添加到容器中

下面開始測試
package com.tabctrlshift.springboot;
import com.tabctrlshift.springboot.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;
/**
* spring boot單元測試
*
* 可以在測試期間類似編碼一樣進行自動注入等容器的功能
*/
//用spring的驅動器來跑,而不是junit
@RunWith(SpringRunner.class)
//springboot的單元測試
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
@Autowired
Person person;
@Test
// 在這運行
public void contextLoads() {
// 簡寫so就可以聯想出來
System.out.println(person);
}
}

運行測試,控制台打印出來ok
Person{lastName='zhangsan', age=18, boos=false, birth=Tue Dec 12 00:00:00 CST 2017, maps={k1=v1, k2=12}, lists=[list, zhaoliu], dog=Dog{name='小狗', age='2'}}

配置文件、javabean,導入配置文件處理器這樣編寫配置就有提示了
@ConfigurationProperties
同樣如果用.properties文件也會有提示

有中文 文件編碼也要改一下

#server.port=8081 #配置文件默認utf-8 person.age=18 person.birth=2017/12/12 person.boos=false person.last-name=張三 person.lists=a,b,c person.maps.k1=v1 person.maps.k2=v2 person.maps.k3=v3 person.dog.name=dog person.dog.age=15
上面是application.properties代碼例子
