SpringBoot配置文件application.properties的使用
一、存放位置分類(lèi)
- 1.當(dāng)前項(xiàng)目根目錄下的config目錄下
- 2.當(dāng)前項(xiàng)目的根目錄下
- 3.resources目錄下的config目錄下
- 4.resources目錄下
按照這上面的順序,4個(gè)配置文件的優(yōu)先級(jí)依次降低。
我們?cè)陧?xiàng)目里面4個(gè)位置分別設(shè)置了各種的application.properties文件。
每個(gè)文件都設(shè)置了各種的端口號(hào),我們啟動(dòng)項(xiàng)目看到我們當(dāng)前使用的端口號(hào)是優(yōu)先級(jí)最高的項(xiàng)目根目錄下config里面的配置。
我們刪除掉項(xiàng)目根目錄下的config目錄,現(xiàn)在項(xiàng)目使用的端口是項(xiàng)目根目錄下的application.properties。
二、自定義存放位置
如果我們不在這4個(gè)位置也想加載我們的配置文件的話,我們可以在resources目錄下創(chuàng)建一個(gè)新目錄命名為myconfig,目錄下存放一個(gè)application.properties文件。
我們可以在打包成jar的情況下在啟動(dòng)命令中加入配置文件的參數(shù),就可以啟動(dòng)自定義的配置。
java -jar xx.jar --spring.config.location=classpath:/myconfig/
三、自定義文件名
我們的application.properties文件名稱也可以修改,比如修改成app.properties。
我們可以在打成jar包的情況下在啟動(dòng)命令下加入配置文件名的參數(shù),就可以使用自定義配置文件名。
java -jar SpringBootDemo-0.0.1-SNAPSHOT.jar --spring.config.name=app
我們看到現(xiàn)在項(xiàng)目使用的端口是app.properties文件下的8081端口了。
四、屬性注入
我們?cè)赼pplication.properties文件中定義屬性:
student.name=zhangsan student.age=20
我們通過(guò)@Value注解把這些屬性注入到我們的Student對(duì)象中:
示例代碼如下:
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author qinxun * @date 2023-06-15 * @Descripion: 學(xué)生實(shí)體類(lèi) */ @Component public class Student { @Value("${student.name}") private String name; @Value("${student.age}") private Integer age; @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
測(cè)試:
import com.example.springbootdemo.bean.Student; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class SpringBootDemoApplicationTests { @Autowired private Student student; @Test void contextLoads() { // 輸出 Student{name='zhangsan', age=20} System.out.println(student); } }
五、類(lèi)型安全的屬性注入
示例代碼如下:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author qinxun * @date 2023-06-15 * @Descripion: 學(xué)生實(shí)體類(lèi) */ @Component @ConfigurationProperties(prefix = "student") public class Student { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
這里我們主要引入@ConfigurationProperties(prefix="student")注解,并且配置了屬性的前綴,此時(shí)自動(dòng)將Spring容器中對(duì)應(yīng)的數(shù)據(jù)注入到對(duì)象對(duì)應(yīng)的屬性中,不用通過(guò)@Value注解一個(gè)個(gè)注入。
配置文件:
student.name=lisi student.age=20
測(cè)試:
import com.example.springbootdemo.bean.Student; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class SpringBootDemoApplicationTests { @Autowired private Student student; @Test void contextLoads() { // 輸出 Student{name='lisi', age=20} System.out.println(student); } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis的@select和@SelectProvider注解方式動(dòng)態(tài)SQL語(yǔ)句解讀
這篇文章主要介紹了Mybatis的@select和@SelectProvider注解方式動(dòng)態(tài)SQL語(yǔ)句,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12獲取JPEGImageEncoder和JPEGCode這兩個(gè)類(lèi)的方法
下面小編就為大家?guī)?lái)一篇獲取JPEGImageEncoder和JPEGCode這兩個(gè)類(lèi)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07SpringBoot使用Mybatis注解實(shí)現(xiàn)分頁(yè)動(dòng)態(tài)sql開(kāi)發(fā)教程
這篇文章主要為大家介紹了SpringBoot使用Mybatis注解實(shí)現(xiàn)分頁(yè)及動(dòng)態(tài)sql開(kāi)發(fā)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03springboot項(xiàng)目實(shí)現(xiàn)配置跨域
這篇文章主要介紹了springboot項(xiàng)目實(shí)現(xiàn)配置跨域問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-09-09