02-springboot配置

目錄

 

1,前言

2,YAML介紹

3,獲取yml配置文件內容

4,springboot的配置文件

5,springboot使用@Value實現映射

6,@PropertySource、@ImportResource和@Bean註解

7,Springboot的佔位符

8,profile

9,配置文件存放目錄優先級(從高到低)

10,總結


1,前言

主要講的有yaml語法,配置文件,配置文件加載順序,配置文件配置原理。

2,YAML介紹

     A:什麼是YAML

           YAML(/ˈjæməl/,尾音類似camel駱駝)是一個可讀性高,用來表達數據序列化的格式。內容是一個鍵值對,所以它是與數據爲中心,更加適合作爲配置文件。文件後綴是以yml結尾,用於springboot的配置文件中,默認的配置文件名爲:application.yml。

   B:YAML語法

         a:基本語法

            K:(空格)V:表示一個鍵值對,在冒號後面必須有一個空格,空格表示的是層級關係,如果多行的時候是對齊的,表示是同級關係:

server:
 port: 8080

注意:屬性和值之間是區分大小寫的;雙引號不會轉義字符(name:  "zhangsan \n lisi":輸出;zhangsan 換行 lisi );

單引號會轉義字符(name: ‘zhangsan \n lisi’:輸出;zhangsan \n lisi );字符串可以不需要引號。

       b:值的寫法

           字面值:

                k: v:字面直接來寫;

           對象。Map:

              k: v:在下一行來寫對象的屬性和值的關係;注意縮進 ,對象還是k: v的方式;

friends:
  lastName: zhangsan 
  age: 20

行內寫法:

friends: {lastName: zhangsan,age: 18}

          

        集合:

用- 值表示數組中的一個元素

     

pets: 
  ‐ cat 
  ‐ dog
  ‐ pig

       行內寫法:

pets: [cat,dog,pig] 

3,獲取yml配置文件內容

     A:編寫兩個實體類Person,Student,用於存放屬性。

Person類:

@Component
@ConfigurationProperties(prefix="Person")
public class Person {
     private int age;
     private String name;
     private Map<String,String> map;
     private List<String> list;
     private Student student;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	@Override
	public String toString() {
		return "Person [age="+ age + ", name=" + name + ", map=" + map + ", list=" + list + ", student=" + student
				+ "]";
	}
     
}

Student類:


public class Student {
	private int age;
    private String name;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [age=" + age + ", name=" + name + "]";
	}
    
}

      編寫yml配置文件:

server:
	port: 8080

Person:
	age: 18
	name: xiaozhi
	map:
		k1: we
		k2: wo
	list:
		-haha
		-hoho
	student:
		age: 20
		name:xiaofang

這是需要將我們通知springboot該類類中的所有屬性和配置文件中相關的配置進行綁定,使用的註解爲:@ConfigurationProperties:該註解有個屬性prefix ,值爲類,配置文件中哪個類下面的所有屬性進行映射。

同時還需要將給類加入到springboot組件,需要的註解爲:@Component

還需要在pom文件添加一個座標,該座標表示的是掃描配置文件,文件校驗,可以不給出,如果不給,編寫yml配置文件不給出提示和校驗。

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring‐boot‐configuration‐processor</artifactId> 
      <optional>true</optional> 
</dependency

B:在主程序運行結果(可使用單元測試)

控制類:

@Controller
public class AppController {
	@Resource
	private Person person;
	@ResponseBody
	@RequestMapping("/show")
   public String show(){
		System.out.print(person);
	   return "你好!世界";
   }
}

測試結果爲:

可以看到Person類的屬性以及被注入了。

4,springboot的配置文件

     A:springboot給出兩種配置文件

           •application.properties

           •application.yml

配置文件默認爲固定文件名,springboot可以將自動將文件掃描到容器中。之前一起講了yml配置文件的編寫方法和加載方式。項目將會說properties 文件的編寫和加載方式;

    B:編寫properties 文件

Person.age=18
Person.name=xiaozhi
Person.map.k1=we
Person.map.k2=wuw
Person.list=we.weo.werf
Person.student.age=20
Person.student.name=xiaofang

只要編寫程這樣就可以了,加載方式和yml的一樣,結果如下圖所示:

5,springboot使用@Value實現映射

 之前使用@ConfigurationProperties(prefix="Person")這個註解實現配置文件到類屬性的映射,@ConfigurationProperties註解表示的是將該類的全部屬性實現映射,只要配置文件有對應的屬性,就將配置文件的內容注入到類中。而@Value是單個注入,分別對類的屬性進行注入。

   A:使用@Value實現注入

 只需要通過修改Person類就可以了,如下段代碼:

@Component
//@ConfigurationProperties(prefix="Person")
public class Person {
	@Value("${Person.age}")
     private int age;
     private String name;
     private Map<String,String> map;
     private List<String> list;
     private Student student;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	@Override
	public String toString() {
		return "Person [age="+ age + ", name=" + name + ", map=" + map + ", list=" + list + ", student=" + student
				+ "]";
	}
     
}

測試結果:

 

可以發現,只是在age屬性上加上@Value時,注入值只是注入這個屬性的值。

  B:@Value和@ConfigurationProperties的比較

6,@PropertySource、@ImportResource和@Bean註解

     A:PropertySource:讀取指定的文件(只適用於Properties文件)

          屬性:value:String[]

寫一個a.Properties文件

Person.age=20
Person.name=xiao
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210
Person.student.name=xiaofan

           在Person類上添加註解

@PropertySource(value={"classpath:a.properties"})
@Component
@ConfigurationProperties(prefix="Person")
public class Person {
	
     private int age;
     private String name;
     private Map<String,String> map;
     private List<String> list;
     private Student student;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	@Override
	public String toString() {
		return "Person [age="+ age + ", name=" + name + ", map=" + map + ", list=" + list + ", student=" + student
				+ "]";
	}
     
}

運行結果:

值得注意的是使用此註解掃描時,如果配置文件目錄有一個application.properties文件,而且該文件中存在該類的配置,它會優先使用默認配置文件而不去加載自定義配置文件。

B:@ImportResource註解:用於加載spring配置文件

       在學習spring的時候,spring的配置文件主要配置的是bean對象,springboot頁提供了一個掃描器用於加載spring的配置文件。該註解就是掃描spring配置文件所使用的。

編寫一個spring的配置文件

<?xml version="1.0" encoding="UTF‐8"?> <beans 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring‐beans.xsd"> 
<bean id="helloService" class="com.atguigu.springboot.service.HelloService"></bean> 
</beans>

在主程序類中加入註解:

@ImportResource(locations = {"classpath:beans.xml"})

就可以將該bean對象注入到sprig容器中。

C:@Bean標籤

spring不建議我們使用@ImportResource註解,主要是會導致開發的週期過長,編寫spring配置文件過於麻煩。爲了解決這個問題,springboot提供@Bean註解,將類注入到spring容器中。使用該註解需要指定需要配置的類,@Configuration註解就可以指定配置類。

@Configuration//指明當前類是一個配置類;就是來替代之前的Spring配置文件
public class Person {
     private int age;
     private String name;
     private Map<String,String> map;
     private List<String> list;
     private Student student;
     @Bean//在配置文件中用<bean><bean/>標籤添加組件
     public void show(){}

7,Springboot的佔位符

springboot在配置文件中可以定義佔位符,通過佔位符設置默認值,獲取該配置文件其他值,拼接一些隨機數等。

A:隨機數

${random.value}、${random.int}、${random.long} 
${random.int(10)}、${random.int[1024,65536]}

springboot自己定義了一些獲取隨機數或者uuid的方法,如下所示:

Person.age=$(random.int)
Person.name=xiao$(random.uuid)
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210
Person.student.name=xiaofan

B:獲取配置文件其他值:

Person.age=20
Person.name=xiao
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210${Person.age}
Person.student.name=xiaofan

表示獲取到Person.age的值拼接到Person.student.age的值的後面,如果配置文件中沒有屬性和值,而配置類中有該屬性,通過配置配置文件中的屬性,將獲取不到該值:

#Person.age=20
Person.name=xiao
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210${Person.age}
Person.student.name=xiaofan

如上面代碼所示,將Person.age註釋掉,將會導致出錯。

C:設置默認值

Person.age=20
Person.name=xiao
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210${Person.hahah}
Person.student.name=xiaofan

這種上面代碼的情況是如果配置文件中的佔位符在配置類沒有屬性,沒有給出默認值,表示的是字符拼接。

Person.age=20
Person.name=xiao
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210${Person.hahah:html}
Person.student.name=xiaofan

這種情況是給該屬性賦值一個默認值,獲取的時候是直接獲取html這個值而不是直接拼接。

8,profile

springboot爲我們提供了在不同環境使用不同配置文件的方式,例如配置端口號,在生產環境下使用8080端口,在測試環境下使用8081端口,開發環境使用8082端口,通過使用profile,可以讓我們快速更換端口號

A:多個Profifile文件

文件命名格式:

application-{profifile}.properties/yml
例如:
application-dev.properties
application-dev.yml

B:properties配置文件使用profile功能

編寫多個properties文件

application-prod.properties
server.port=8080

application-dev.properties

server.port=8081

激活profile:在默認配置文件application.properties中配置:

spring.profiles.active=dev

表示的是使用dev環境,也就是端口號爲8081的配置。

C:yml配置文件使用profile

   這種方式比較簡單,只需要編寫在一個yml文件就可以了,這種方式在yml被叫做多文檔塊,通過"---"將一個yml文件分爲多個文檔塊(三個橫線),在最上面通過最上面的文檔塊進行激活:

spring: 
  profiles: 
    active: prod

全部的配置文件如下所示:

server:
 port: 8081 
spring: 
  profiles: 
    active: prod #激活prod環境
‐‐‐ 
server: 
  port: 8083 
spring: 
  profiles: dev 
‐‐‐ 
server: 
  port: 8084 
spring: 
profiles: prod #指定屬於哪個環境

9,配置文件存放目錄優先級(從高到低)

項目/config

項目目錄項

classpath:config

classpath目錄

10,總結

沒有總結的總結

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章