Spring Boot兩種全局配置和兩種注解的操作方法
1、掌握application.properties配置文件
2、掌握application.yaml配置文件
3、掌握使用@ConfigurationProperties注入屬性
4、掌握使用@Value注入屬性
一、全局配置文件概述全局配置文件能夠?qū)σ恍┠J(rèn)配置值進(jìn)行修改。Spring Boot使用一個(gè)application.properties或者application.yaml的文件作為全局配置文件,該文件存放在src/main/resource目錄或者類路徑的/config,一般會(huì)選擇resource目錄。
二、Application.properties配置文件(一)創(chuàng)建Spring Boot的Web項(xiàng)目PropertiesDemo
利用Spring Initializr方式創(chuàng)建項(xiàng)目
設(shè)置項(xiàng)目元數(shù)據(jù)
添加測(cè)試和Web依賴
設(shè)置項(xiàng)目名稱及保存位置
單擊【Finish】按鈕,完成項(xiàng)目初始化工作
設(shè)置項(xiàng)目編碼為utf8
(二)在application.properties里添加相關(guān)配置 點(diǎn)開resource目錄,查看應(yīng)用程序?qū)傩耘渲梦募?/p>
#修改tomcat默認(rèn)端口號(hào)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啟動(dòng)應(yīng)用,查看控制臺(tái)
(1)創(chuàng)建Pet類
在net.hw.lesson03里創(chuàng)建bean子包,在子包里創(chuàng)建Pet類
package net.hw.lesson03.bean; /** * 功能:寵物實(shí)體類 * 作者:華衛(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; // 編號(hào) 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里配置對(duì)象
#配置對(duì)象person.id=1person.name=張三豐person.hobby=旅游,美食,音樂(lè)person.family.father=張?jiān)乒鈖erson.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方法,才會(huì)自動(dòng)為Person類所有屬性注入相應(yīng)的值,包括簡(jiǎn)單類型和復(fù)雜類型
(5)給Pet類添加注解
添加注解@Component,交給Spring去管理 添加注解@ConfigurationProperties(prefix = “person.pet”) - 可以不用添加(6)從Spring容器里獲取Person類的實(shí)例并輸出
實(shí)現(xiàn)接口ApplicationContextAware,實(shí)現(xiàn)其抽象方法setApplicationContext
聲明ApplicationContext對(duì)象,并在setApplicationContext里初始化
創(chuàng)建測(cè)試方法testPerson(),從Spring容器中獲取Person類的實(shí)例并輸出
運(yùn)行測(cè)試方法testPerson(),查看結(jié)果
(7)解決輸出結(jié)果的漢字亂碼問(wèn)題
使用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編碼形式
運(yùn)行測(cè)試方法testPerson(),查看結(jié)果
(8)從Spring容器里獲取Pet類的實(shí)例并輸出
查看Pet類的注解,有配置屬性的注解@ConfigurationProperties(prefix = 'person.pet')
在測(cè)試類里添加測(cè)試方法testPet()
運(yùn)行測(cè)試方法testPet(),查看結(jié)果
注釋掉Pet類的配置屬性的注解@ConfigurationProperties(prefix = 'person.pet')
再次運(yùn)行測(cè)試方法testPet(),查看結(jié)果
修改application.properties,配置寵物對(duì)象
再次運(yùn)行測(cè)試方法testPet(),查看結(jié)果
大家可以看到,寵物對(duì)象的屬性依然沒有被注入,下面我們換一種屬性注解的方式,采用@Value注解方式。
給Pet類的屬性添加值注解@Value
再次運(yùn)行測(cè)試方法testPet(),查看結(jié)果
1、備份application.properties文件 文件更名為application.back,即讓此文件不起作用
2、在resoures目錄里創(chuàng)建application.yaml文件
創(chuàng)建application.yaml文件
配置服務(wù)器屬性
配置person對(duì)象屬性
配置pet對(duì)象屬性
查看application.yaml文件內(nèi)容
#配置服務(wù)器server: port: 8888 servlet: context-path: /lzy #配置person對(duì)象person: id: 1 name: 張三豐 hobby: 旅游 美食 音樂(lè) family: { father: 張?jiān)乒? mother: 吳文燕, grandpa: 張宏宇, grandma: 唐雨欣, son: 張君寶, daughter: 張曉敏 } pet: type: 泰迪犬 name: 瑞瑞 #配置pet對(duì)象pet: type: 泰迪犬 name: 瑞瑞
3、運(yùn)行測(cè)試方法testPerson(),查看結(jié)果
4、運(yùn)行測(cè)試方法testPet(),查看結(jié)果
1、application.properties配置文件
采用XML語(yǔ)法,鍵值對(duì):鍵=值,沒有層次結(jié)構(gòu) 如果值里有漢字,必須得轉(zhuǎn)成unicode,否則會(huì)出現(xiàn)亂碼問(wèn)題2、application.yaml配置文件
采用YAML語(yǔ)法,鍵值對(duì):鍵: 值(冒號(hào)與值之間有空格),具有層次結(jié)構(gòu) 允許值里有漢字,不必轉(zhuǎn)成unicode,也不會(huì)出現(xiàn)亂碼問(wèn)題五、課后作業(yè)任務(wù):修改StudentInfo項(xiàng)目輸出學(xué)生信息
創(chuàng)建學(xué)生實(shí)體類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對(duì)象屬性
在瀏覽器里訪問(wèn)http://localhost:8080/student
到此這篇關(guān)于Spring Boot兩種全局配置和兩種注解的文章就介紹到這了,更多相關(guān)Spring Boot配置注解內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法2. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析3. 淺談python出錯(cuò)時(shí)traceback的解讀4. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法5. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼6. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解7. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題8. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向9. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解10. Nginx+php配置文件及原理解析
