Spring Boot兩種全局配置和兩種注解的操作方法
零、學(xué)習(xí)目標(biāo)
1、掌握application.properties配置文件
2、掌握application.yaml配置文件
3、掌握使用@ConfigurationProperties注入屬性
4、掌握使用@Value注入屬性
一、全局配置文件概述
全局配置文件能夠?qū)σ恍┠J(rèn)配置值進(jìn)行修改。Spring Boot
使用一個application.properties
或者application.yaml
的文件作為全局配置文件,該文件存放在src/main/resource
目錄或者類路徑的/config
,一般會選擇resource
目錄。
二、Application.properties配置文件
(一)創(chuàng)建Spring Boot的Web項目PropertiesDemo
利用Spring Initializr方式創(chuàng)建項目
設(shè)置項目元數(shù)據(jù)
添加測試和Web依賴
設(shè)置項目名稱及保存位置
單擊【Finish】按鈕,完成項目初始化工作
設(shè)置項目編碼為utf8
(二)在application.properties里添加相關(guān)配置 點開resource目錄,查看應(yīng)用程序?qū)傩耘渲梦募?/p>
1、配置tomcat端口號和web虛擬路徑
#修改tomcat默認(rèn)端口號 server.port=8888 #修改web虛擬路徑 server.servlet.context-path=/lzy
更多配置屬性,詳見官網(wǎng)https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html啟動應(yīng)用,查看控制臺
2、對象類型的配置與使用
(1)創(chuàng)建Pet類
在net.hw.lesson03里創(chuàng)建bean子包,在子包里創(chuàng)建Pet類
package net.hw.lesson03.bean; /** * 功能:寵物實體類 * 作者:華衛(wèi) * 日期:2021年04月28日 */ public class Pet { private String type; // 類型 private String name; // 名字 public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Pet{" + "type='" + type + '\'' + ", name='" + name + '\'' + '}'; } }
(2)創(chuàng)建Person類
在net.hw.lesson03.bean包里創(chuàng)建Person類
package net.hw.lesson03.bean; import java.util.List; import java.util.Map; /** * 功能:人類 * 作者:華衛(wèi) * 日期:2021年04月28日 */ public class Person { private int id; // 編號 private String name; // 姓名 private List<String> hobby; // 愛好; private Map<String, String> family; // 家庭成員 private Pet pet; // 寵物 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getHobby() { return hobby; } public void setHobby(List<String> hobby) { this.hobby = hobby; } public Map<String, String> getFamily() { return family; } public void setFamily(Map<String, String> family) { this.family = family; } public Pet getPet() { return pet; } public void setPet(Pet pet) { this.pet = pet; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", hobby=" + hobby + ", family=" + family + ", pet=" + pet + '}'; } }
(3)在application.properties里配置對象
#配置對象 person.id=1 person.name=張三豐 person.hobby=旅游,美食,音樂 person.family.father=張云光 person.family.mother=吳文燕 person.family.grandpa=張宏宇 person.famliy.grandma=唐雨欣 person.family.son=張君寶 person.family.daughter=張曉敏 person.pet.type=泰迪犬 person.pet.name=瑞瑞
(4)給Person類添加注解
添加注解@Component,交給Spring去管理
添加注解@ConfigurationProperties(prefix = “person”)
注意:采用@ConfigurationProperties
注解方式,必須要有set方法
,才會自動為Person類所有屬性注入相應(yīng)的值,包括簡單類型和復(fù)雜類型
(5)給Pet類添加注解
- 添加注解@Component,交給Spring去管理
- 添加注解@ConfigurationProperties(prefix = “person.pet”) - 可以不用添加
(6)從Spring容器里獲取Person類的實例并輸出
實現(xiàn)接口ApplicationContextAware,實現(xiàn)其抽象方法setApplicationContext
聲明ApplicationContext對象,并在setApplicationContext里初始化
創(chuàng)建測試方法testPerson(),從Spring容器中獲取Person類的實例并輸出
運行測試方法testPerson(),查看結(jié)果
(7)解決輸出結(jié)果的漢字亂碼問題
使用JDK工具native2ascii.exe
將漢字處理成uncode
編碼
D:\IdeaProjects\PropertiesDemo>cd src/main/resources D:\IdeaProjects\PropertiesDemo\src\main\resources>native2ascii -encoding utf8 application.properties #\u4fee\u6539tomcat\u9ed8\u8ba4\u7aef\u53e3\u53f7 server.port=8888 #\u4fee\u6539web\u865a\u62df\u8def\u5f84 server.servlet.context-path=/lzy #\u914d\u7f6e\u5bf9\u8c61 person.id=1 person.name=\u5f20\u4e09\u4e30 person.hobby=\u65c5\u6e38,\u7f8e\u98df,\u97f3\u4e50 person.family.father=\u5f20\u4e91\u5149 person.family.mother=\u5434\u6587\u71d5 person.family.grandpa=\u5f20\u5b8f\u5b87 person.famliy.grandma=\u5510\u96e8\u6b23 person.family.son=\u5f20\u541b\u5b9d person.family.daughter=\u5f20\u6653\u654f person.pet.type=\u6cf0\u8fea\u72ac person.pet.name=\u745e\u745e D:\IdeaProjects\PropertiesDemo\src\main\resources>
修改application.properties文件,漢字采用unicode編碼形式
運行測試方法testPerson(),查看結(jié)果
(8)從Spring容器里獲取Pet類的實例并輸出
查看Pet類的注解,有配置屬性的注解@ConfigurationProperties(prefix = "person.pet")
在測試類里添加測試方法testPet()
運行測試方法testPet(),查看結(jié)果
注釋掉Pet類的配置屬性的注解@ConfigurationProperties(prefix = "person.pet")
再次運行測試方法testPet(),查看結(jié)果
修改application.properties,配置寵物對象
再次運行測試方法testPet(),查看結(jié)果
大家可以看到,寵物對象的屬性依然沒有被注入,下面我們換一種屬性注解的方式,采用@Value注解方式。
給Pet類的屬性添加值注解@Value
再次運行測試方法testPet(),查看結(jié)果
3、兩種屬性注解方式的對比
- 采用
@ConfigurationProperties
注解方式,必須要有set方法
,才會自動為所注解的類的全部屬性注入相應(yīng)的值,包括簡單類型和復(fù)雜類型(List、Map、Pet……)。 - 采用
@Value
注解方式,優(yōu)點在于可以不要set方法
,但是有兩點不足:其一、需要一個一個地注入,顯得麻煩;其二、對于復(fù)雜類型不能注入,比如Map、List、Pet等。
三、Application.yaml配置文件
1、備份application.properties文件 文件更名為application.back
,即讓此文件不起作用
2、在resoures目錄里創(chuàng)建application.yaml文件
創(chuàng)建application.yaml文件
配置服務(wù)器屬性
配置person對象屬性
配置pet對象屬性
查看application.yaml文件內(nèi)容
#配置服務(wù)器 server: port: 8888 servlet: context-path: /lzy #配置person對象 person: id: 1 name: 張三豐 hobby: 旅游 美食 音樂 family: { father: 張云光, mother: 吳文燕, grandpa: 張宏宇, grandma: 唐雨欣, son: 張君寶, daughter: 張曉敏 } pet: type: 泰迪犬 name: 瑞瑞 #配置pet對象 pet: type: 泰迪犬 name: 瑞瑞
3、運行測試方法testPerson(),查看結(jié)果
4、運行測試方法testPet(),查看結(jié)果
四、兩種配置文件的比較
1、application.properties配置文件
- 采用XML語法,鍵值對:鍵=值,沒有層次結(jié)構(gòu)
- 如果值里有漢字,必須得轉(zhuǎn)成unicode,否則會出現(xiàn)亂碼問題
2、application.yaml配置文件
- 采用YAML語法,鍵值對:鍵: 值(冒號與值之間有空格),具有層次結(jié)構(gòu)
- 允許值里有漢字,不必轉(zhuǎn)成unicode,也不會出現(xiàn)亂碼問題
五、課后作業(yè)
任務(wù):修改StudentInfo項目輸出學(xué)生信息
創(chuàng)建學(xué)生實體類Student
添加屬性
private String id; private String name; private String gender; private int age; private String major; private String telephone; private String email; private String hobby;
- 添加getter和setter
- 添加toString()方法
- 添加注解@Component
- 添加注解@ConfigureProperties
- 將application.properties更名為application.yaml
配置student對象屬性
- 修改控制器StudentInfoController,student()方法里返回student的值
- 運行啟動類
在瀏覽器里訪問http://localhost:8080/student
到此這篇關(guān)于Spring Boot兩種全局配置和兩種注解的文章就介紹到這了,更多相關(guān)Spring Boot配置注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用elasticsearch分組進(jìn)行聚合查詢過程解析
這篇文章主要介紹了java使用elasticsearch分組進(jìn)行聚合查詢過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02SpringBoot使用Prometheus實現(xiàn)監(jiān)控
在當(dāng)今的軟件開發(fā)世界中,監(jiān)控是至關(guān)重要的一部分,本文主要介紹了如何在Spring Boot應(yīng)用程序中使用Prometheus進(jìn)行監(jiān)控,以幫助大家更好地理解和管理您的應(yīng)用程序,有需要的可以參考下2023-10-10