Spring Boot兩種全局配置和兩種注解的操作方法
1、掌握application.properties配置文件
2、掌握application.yaml配置文件
3、掌握使用@ConfigurationProperties注入屬性
4、掌握使用@Value注入屬性
一、全局配置文件概述全局配置文件能夠對一些默認配置值進行修改。Spring Boot使用一個application.properties或者application.yaml的文件作為全局配置文件,該文件存放在src/main/resource目錄或者類路徑的/config,一般會選擇resource目錄。
二、Application.properties配置文件(一)創建Spring Boot的Web項目PropertiesDemo
利用Spring Initializr方式創建項目
設置項目元數據
添加測試和Web依賴
設置項目名稱及保存位置
單擊【Finish】按鈕,完成項目初始化工作
設置項目編碼為utf8
(二)在application.properties里添加相關配置 點開resource目錄,查看應用程序屬性配置文件
#修改tomcat默認端口號server.port=8888#修改web虛擬路徑server.servlet.context-path=/lzy
更多配置屬性,詳見官網https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html啟動應用,查看控制臺
(1)創建Pet類
在net.hw.lesson03里創建bean子包,在子包里創建Pet類
package net.hw.lesson03.bean; /** * 功能:寵物實體類 * 作者:華衛 * 日期: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)創建Person類
在net.hw.lesson03.bean包里創建Person類
package net.hw.lesson03.bean; import java.util.List;import java.util.Map; /** * 功能:人類 * 作者:華衛 * 日期: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=1person.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類所有屬性注入相應的值,包括簡單類型和復雜類型
(5)給Pet類添加注解
添加注解@Component,交給Spring去管理 添加注解@ConfigurationProperties(prefix = “person.pet”) - 可以不用添加(6)從Spring容器里獲取Person類的實例并輸出
實現接口ApplicationContextAware,實現其抽象方法setApplicationContext
聲明ApplicationContext對象,并在setApplicationContext里初始化
創建測試方法testPerson(),從Spring容器中獲取Person類的實例并輸出
運行測試方法testPerson(),查看結果
(7)解決輸出結果的漢字亂碼問題
使用JDK工具native2ascii.exe將漢字處理成uncode編碼
D:IdeaProjectsPropertiesDemo>cd src/main/resources D:IdeaProjectsPropertiesDemosrcmainresources>native2ascii -encoding utf8 application.properties#u4feeu6539tomcatu9ed8u8ba4u7aefu53e3u53f7server.port=8888#u4feeu6539webu865au62dfu8defu5f84server.servlet.context-path=/lzy #u914du7f6eu5bf9u8c61person.id=1person.name=u5f20u4e09u4e30person.hobby=u65c5u6e38,u7f8eu98df,u97f3u4e50person.family.father=u5f20u4e91u5149person.family.mother=u5434u6587u71d5person.family.grandpa=u5f20u5b8fu5b87person.famliy.grandma=u5510u96e8u6b23person.family.son=u5f20u541bu5b9dperson.family.daughter=u5f20u6653u654fperson.pet.type=u6cf0u8feau72acperson.pet.name=u745eu745e D:IdeaProjectsPropertiesDemosrcmainresources>
修改application.properties文件,漢字采用unicode編碼形式
運行測試方法testPerson(),查看結果
(8)從Spring容器里獲取Pet類的實例并輸出
查看Pet類的注解,有配置屬性的注解@ConfigurationProperties(prefix = 'person.pet')
在測試類里添加測試方法testPet()
運行測試方法testPet(),查看結果
注釋掉Pet類的配置屬性的注解@ConfigurationProperties(prefix = 'person.pet')
再次運行測試方法testPet(),查看結果
修改application.properties,配置寵物對象
再次運行測試方法testPet(),查看結果
大家可以看到,寵物對象的屬性依然沒有被注入,下面我們換一種屬性注解的方式,采用@Value注解方式。
給Pet類的屬性添加值注解@Value
再次運行測試方法testPet(),查看結果
1、備份application.properties文件 文件更名為application.back,即讓此文件不起作用
2、在resoures目錄里創建application.yaml文件
創建application.yaml文件
配置服務器屬性
配置person對象屬性
配置pet對象屬性
查看application.yaml文件內容
#配置服務器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(),查看結果
4、運行測試方法testPet(),查看結果
1、application.properties配置文件
采用XML語法,鍵值對:鍵=值,沒有層次結構 如果值里有漢字,必須得轉成unicode,否則會出現亂碼問題2、application.yaml配置文件
采用YAML語法,鍵值對:鍵: 值(冒號與值之間有空格),具有層次結構 允許值里有漢字,不必轉成unicode,也不會出現亂碼問題五、課后作業任務:修改StudentInfo項目輸出學生信息
創建學生實體類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對象屬性
在瀏覽器里訪問http://localhost:8080/student
到此這篇關于Spring Boot兩種全局配置和兩種注解的文章就介紹到這了,更多相關Spring Boot配置注解內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
