??哈嘍,大家好,我是小浪。上篇博客我們已經(jīng)學(xué)習(xí)了如何創(chuàng)建一個(gè)Spring項(xiàng)目,那么創(chuàng)建Spirng項(xiàng)目還可以直接通過(guò)在Spring官網(wǎng)的方式來(lái)創(chuàng)建,做法也非常的簡(jiǎn)單,感興趣的小伙伴可以在C站搜個(gè)教程嘗試一下;那么,今天我們就來(lái)學(xué)習(xí)SpringBoot如何配置文件;????
??目錄
一、為什么要配置文件,配置文件的作用?
二、SpringBoot配置文件的方法
二、SpringBoot配置文件的格式
一、properties
二、yml
三、讀取配置文件
1、properties
2、yml
3、配置對(duì)象
一、為什么要配置文件,配置文件的作用?

二、SpringBoot配置文件的方法
1、系統(tǒng)使用的配置文件,如端口號(hào)的設(shè)置,連接數(shù)據(jù)庫(kù)的配置。
2、用戶自定義的配置文件。
二、SpringBoot配置文件的格式
一、properties
1、當(dāng)我們沒(méi)有設(shè)置配置文件的時(shí)候,我們的SpringBoot默認(rèn)啟動(dòng)的端口號(hào)是8080;
2 、當(dāng)我們通過(guò)properties來(lái)設(shè)置一個(gè)端口號(hào)時(shí),我們啟動(dòng)springboot項(xiàng)目來(lái)觀察一下;
2.1在resources目錄下新建一個(gè)文件applications.properties;
2.2這里我們可以自己設(shè)置任意一個(gè)端口號(hào),比如我們給個(gè)8888;
server.port=8888
再次運(yùn)行觀察結(jié)果:
結(jié)果可以看到,我們自己設(shè)置的端口號(hào)生效了;
那么如何訪問(wèn)到呢?
我們之前在demo包下面新建了一個(gè)TestController類,內(nèi)容如下:
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Controller//當(dāng)前類是控制器
@ResponseBody//返回的是數(shù)據(jù),而非頁(yè)面
public class TestController {
@RequestMapping("/hello")//url路由注冊(cè)
public String sayHi(String name){
if(!StringUtils.hasLength(name)){
name = "SpringBoot";
}
return "hello"+ name;
}
}
這個(gè)時(shí)候我們就可以通過(guò)瀏覽器來(lái)訪問(wèn),在瀏覽器中輸入以下內(nèi)容:
http://localhost:8888/hello
按下回車,觀察運(yùn)行結(jié)果:
二、yml
1、yml 是 YAML 是縮寫,它的全稱 Yet Another Markup Language 翻譯成中?就是 “另?種標(biāo)記語(yǔ)言”。
yml 是樹形結(jié)構(gòu)的配置文件,它的基礎(chǔ)語(yǔ)法是“key: value”,注意 key 和 value 之間使?英?冒汗加空 格的方式組成的,其中的空格不可省略;
2、首先還是在resources目錄下新建一個(gè)yml文件,格式如下:
yml文件代碼如下,我們來(lái)測(cè)試一下新設(shè)置的端口號(hào)是否生效;
3、在瀏覽器輸入:http://localhost:7777/hello 按下回車,看運(yùn)行結(jié)果;
結(jié)果依然是沒(méi)有問(wèn)題;
注:理論上講 properties 可以和 yml ?起存在于?個(gè)項(xiàng)?當(dāng)中,當(dāng) properties 和 yml ?起存在?個(gè)項(xiàng)目中時(shí),如果配置?件中出現(xiàn)了同樣的配置,比如 properties 和 yml 中都配置“server.port”, 那么這個(gè)時(shí)候會(huì)以 properties 中的配置為主,也就是 .properties 配置?件的優(yōu)先級(jí)最高,但加載完 .properties ?件之后,也會(huì)加載 .yml 文件的配置信息。
三、讀取配置文件
1、properties
server:
port: 7777
#自定義配置項(xiàng)
mytest: 小黑
TestController:
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Controller//當(dāng)前類是控制器
@ResponseBody//返回的是數(shù)據(jù),而非頁(yè)面
public class TestController {
@Value("${mytest}")
private String mytest;
@RequestMapping("/hello")//url路由注冊(cè)
public String sayHi(String name){
if(!StringUtils.hasLength(name)){
name = "SpringBoot";
}
return "hello"+ name;
}
@RequestMapping("/getconf")
public String getconf(){
return mytest;
}
}
首先啟動(dòng)springboot類,啟動(dòng)成功后,再去瀏覽器訪問(wèn)對(duì)應(yīng)的地址;
瀏覽器輸入以下內(nèi)容:http://localhost:7777/getconf? 按下回車:
2、yml
2.1 value值加單雙引號(hào);
application.yml文件寫入以下內(nèi)容:
#字符串
myStirng1: 哈嘍\n呀
myStirng2: '哈嘍\n呀'
myStirng3: "哈嘍\n呀"
TestController類中代碼:
@Value("${myString1}")
private String myString1;
@Value("${myString2}")
private String myString2;
@Value("${myString3}")
private String myString3;
@PostConstruct
public void PostConstruct(){
System.out.println("myString1"+myString1);
System.out.println("myString2"+myString2);
System.out.println("myString3"+myString3);
}
啟動(dòng)springboot項(xiàng)目,觀察結(jié)果;
得以下結(jié)論:
1、字符串默認(rèn)不用加上單引號(hào)或者雙引號(hào)。2、單引號(hào)不會(huì)轉(zhuǎn)義特殊字符,特殊字符最終只是?個(gè)普通的字符串?dāng)?shù)據(jù)。3、雙引號(hào)會(huì)轉(zhuǎn)義字符串里面的特殊字符;特殊字符會(huì)作為本身想表示的意思。
3、配置對(duì)象
1、我們還可以在yml中配置對(duì)象,如下格式:
student:
id: 1
name: zhangsan
age: 18
還可以使用行內(nèi)寫法,將對(duì)象中的屬性寫到一行,不用另起很多行;
student1: {id: 1,name: zhangsan,age: 20}
那么讀取對(duì)象的話就不能使用注解@Value了,需要用到另外一個(gè)注解@ConfigurationProperties 來(lái)讀取;
在demo包下新建一個(gè)類Student:
package com.example.demo;
import lombok.Data;
import lombok.NonNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("student")
@Data
public class Student {
private int id;
@NonNull
private String name;
private int age;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Student(int id, @NonNull String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
TestController中代碼:
@Component
public class ReadYml2 {
@Autowired
private Student student;
@PostConstruct
public void postConstruct() {
System.out.println(student);
}
}
運(yùn)行結(jié)果:
?注意:1、學(xué)生類Student使用@ConfigurationProperties("student")這個(gè)注解,并且注解后面的對(duì)象名必須和配置文件中的一致;
??2、實(shí)體類的屬性名必須和配置中的key保持一致,并且提供getter和setter方法;文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-447610.html
??OK,今天的學(xué)習(xí)內(nèi)容就到這里啦,喜歡的小伙伴可以三連一下,訂閱本專欄,以便于及時(shí)收到更新信息,感謝閱讀,我們下期再見??!????文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-447610.html
到了這里,關(guān)于【Spring框架全系列】SpringBoot配置文件相關(guān)操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!