1.會使用兩種配置文件:
Application.properties,
Application.yml,或者yaml也可以。
配置文件得名字是固定得。
作用是修改spring boot自動配置的默認配置。
2.yaml的寫法:
Server:
Port: 8081
基本語法:
Key : (必須有空格)
對象的寫法:
數組的寫法:
當然,以上得可以混合寫,什麼數組,對象都可以混合寫。
Yaml配置文件的值的獲取
項目結構是這樣的。
Person類:這是一個組件,
將配置文件中配置的每一個屬性的值都映射到這個組件中。
@ConfigurationProperties(prefix = "person"),告訴
Spring Boot,將配置文件中得每一個值和這個組件進行綁定。
Prifix=”person”,與配置文件中對應的模塊進行綁定。
這個註釋,必須在容器中纔可以,即必須得有@Component註釋。
就這樣,編譯器會報錯,上面會有一行報錯信息,提示…..
點進去藍色的鏈接,提示需要導入依賴,這樣以後使用配置文件映射功能
就會有提示了。
@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;
Dog類:
private Dog dog;
public class Dog { private String name; @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + '}'; } private Integer age;
需要得依賴:
<!--導入配置文件處理器,將配置文件進行綁定就會有提示-->
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
Application.yaml:
person:
lastName: zhangsan age: 18 boss: false birth: 2019//4/02 maps: {k1: v1,k2: 12} lists: - lisi - zhaoliu dog: name: xiaog age: 12
運行測試類:
package com.jiangyi.springboot01helloworldquick;
import com.jiangyi.springboot01helloworldquick.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 單元測試 * 可以在測試期間很方便得進行類似編碼方式得自動注入得功能 */ @RunWith(SpringRunner.class)//意識是用spring boot的驅動器來跑,而不是以前得junit @SpringBootTest public class SpringBoot01HelloworldQuickApplicationTests { @Autowired Person person; @Test public void contextLoads() { System.out.println(person); } }
得到結果:
用application.properties來配置
會有亂碼,默認idea使用utf-8來編碼,而properties文件使用ansci碼來
編碼。
解決辦法:在設置settings—》查找框裏輸入file enc,勾選相應得選項。
另外一種獲取值的方式
鬆散綁定:
使用JSR303校驗:
@Validated在類上使用
@ConfigurationProperties和@Value兩種的區別:
配置文件寫什麼都ok,數據都可以拿到。
但是,複雜類型封裝,@Value不可以!比如集合:Map,list
在有的時候使用@Value比較方便,比如:
那麼要在什麼時候使用這兩種呢(區別的使用)
@PropertySource和@ImportResource
@ConfigurationProperties(prefix = "person")
默認從全局裏獲得變量的值,即從那兩個配置文件中獲取。
如果我們要從resource文件夾下的我們自己寫的properties的文件中
獲取數據,則不能用那個註釋!
可以加載到指定的配置文件。
@ImportResource
Spring Boot裏面沒有spring的配置文件,我們自己b編寫的也不能
識別。想要讓Spring的配置文件生效,要用@ImportResource標註在一個
配置類上。(這裏標註在主類上)
---》這個不常用!
一般使用配置類的方式!
創建一個config.MyAppConfig.class
/**
* @Configuration指明當前類是一個配置 * 類,就是來替代之前的 Spring配置文件 * 在配置文件中使用<bean></bean> * 來添加組件 */ @Configuration public class MyAppConfig { //將方法中的返回值添加到容器中 //容器中這個組件的默認的id //就是方法名 @Bean public helloService helloService2(){ System.out.println("配置類@Bean給容器中添加組件了"); return new helloService(); //這裏返回這個方法的方法名helloService } }
在測試類裏檢測是否注入成功,
這裏在配置類裏使用helloService2作爲方法名,故打印出來的是失敗false。
如果修改爲helloService,則打印出來的是成功。
配置文件佔位符
Ex:
person.last-name=江毅${random.uuid}
person.age=${random.int} person.birth=2019/4/02 person.boss=true person.maps.k1=v1 person.maps..k2=v2 person.lists=a,b,c person.dog.name=${person.hello:hello}_dog person.dog.age=3
結果: