淺談Spring Boot 屬性配置和自定義屬性配置
在使用spring boot過(guò)程中,可以發(fā)現(xiàn)項(xiàng)目中只需要極少的配置就能完成相應(yīng)的功能,這歸功于spring boot中的模塊化配置,在pom.xml中依賴的每個(gè)Starter都有默認(rèn)配置,而這些默認(rèn)配置足以滿足正常的功能開(kāi)發(fā)。
如果需要修改自定義修改默認(rèn)配置,spring boot 提供了很簡(jiǎn)便的方法,只需要在application.properties 中添加修改相應(yīng)的配置。(spring boot啟動(dòng)的時(shí)候會(huì)讀取application.properties這份默認(rèn)配置)
一、修改默認(rèn)配置
例1、spring boot 開(kāi)發(fā)web應(yīng)用的時(shí)候,默認(rèn)tomcat的啟動(dòng)端口為8080,如果需要修改默認(rèn)的端口,則需要在application.properties 添加以下記錄:
server.port=8888
重啟項(xiàng)目,啟動(dòng)日志可以看到:Tomcat started on port(s): 8888 (http) 啟動(dòng)端口為8888,瀏覽器中訪問(wèn) http://localhost:8888 能正常訪問(wèn)。
例2、spring boot 開(kāi)發(fā)中的數(shù)據(jù)庫(kù)連接信息配置(這里使用com.alibaba 的 druid), 在application.properties 添加以下記錄:
druid.url=jdbc:mysql://192.168.0.20:3306/test druid.driver-class=com.mysql.jdbc.Driver druid.username=root druid.password=123456 druid.initial-size=1 druid.min-idle=1 druid.max-active=20 druid.test-on-borrow=true
以上兩個(gè)例子,說(shuō)明了如需修改starter模塊中的默認(rèn)配置,只需要在在application.properties 添加需要修改的配置即可。
附: application.properties 全部配置項(xiàng),點(diǎn)擊查看Spring Boot 所有配置說(shuō)明
二、自定義屬性配置
在application.properties中除了可以修改默認(rèn)配置,我們還可以在這配置自定義的屬性,并在實(shí)體bean中加載出來(lái)。
1、在application.properties中添加自定義屬性配置
com.sam.name=sam com.sam.age=11 com.sam.desc=magical sam
2、編寫(xiě)B(tài)ean類(lèi),加載屬性
Sam類(lèi)需要添加@Component注解,讓spring在啟動(dòng)的時(shí)候掃描到該類(lèi),并添加到spring容器中。
第一種:使用spring支持的@Value()加載
package com.sam.demo.conf;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author sam
* @since 2017/7/15
*/
@Component
public class Sam {
//獲取application.properties的屬性
@Value("${com.sam.name}")
private String name;
@Value("${com.sam.age}")
private int age;
@Value("${com.sam.desc}")
private String desc;
//getter & setter
}
第二種:使用@ConfigurationProperties(prefix="") 設(shè)置前綴,屬性上不需要添加注解。
package com.sam.demo.conf;
import org.springframework.stereotype.Component;
/**
* @author sam
* @since 2017/7/15
*/
@Component
@ConfigurationProperties(prefix = "com.sam")
public class Sam {
private String name;
private int age;
private String desc;
//getter & setter
}
3、在controller中注入并使用Sam這個(gè)Bean。
package com.sam.demo.controller;
import com.sam.demo.conf.Sam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author sam
* @since 2017/7/14
*/
@RestController
public class IndexController {
@Autowired
private Sam sam;
@RequestMapping("/index")
public String index() {
System.out.println(sam.getName() + " " + sam.getAge() + " " + sam.getDesc());
return "index";
}
}
瀏覽器訪問(wèn):http://localhost:8080/index ,控制臺(tái)正常打印出sam的內(nèi)容。
三、application.properties 屬性配置詳解
1、參數(shù)引用與random隨機(jī)數(shù)方法的使用
在application.properties內(nèi)可以直接通過(guò)${}引用其他屬性的值,如下:
com.sam.name=sam
com.sam.age=11
com.sam.desc=${name} is ${age} years old.
在application.properties中如果需要獲取隨機(jī)數(shù),可以通過(guò)${random},如下:
#獲取隨機(jī)字符串
com.sam.randomValue=${random.value}
#獲取隨機(jī)字符串:${random.value}
#獲取隨機(jī)int:${random.int}
#獲取10以內(nèi)的隨機(jī)數(shù):${random.int(10)}
#獲取10-20的隨機(jī)數(shù):${random.int[10,20]}
#獲取隨機(jī)long:${random.long}
#獲取隨機(jī)uuid:${random.uuid}
2、多環(huán)境配置
實(shí)際開(kāi)發(fā)中可能會(huì)有不同的環(huán)境,有開(kāi)發(fā)環(huán)境、測(cè)試環(huán)境、生成環(huán)境。對(duì)于每個(gè)環(huán)境相關(guān)配置都可能有所不同,如:數(shù)據(jù)庫(kù)信息、端口配置、本地路徑配置等。
如果每次切換不同環(huán)境都需要修改application.properties,那么操作是十分繁瑣的。在spring boot中提供了多環(huán)境配置,使得我們切換環(huán)境變得簡(jiǎn)便。
在application.properties同目錄下新建一下三個(gè)文件:
application-dev.properties //開(kāi)發(fā)環(huán)境的配置文件 application-test.properties //測(cè)試環(huán)境的配置文件 application-prod.properties //生產(chǎn)環(huán)境的配置文件
上面三個(gè)文件分別對(duì)應(yīng)了 開(kāi)發(fā)、測(cè)試、生產(chǎn) 的配置內(nèi)容,接下來(lái)就是應(yīng)該怎么選擇性引用這些配置了。
在application.properties添加:
spring.profiles.active=dev #引用測(cè)試的配置文件 #spring.profiles.active=test #引用生產(chǎn)的配置文件 #spring.profiles.active=prod
添加spring.profiles.active=dev后啟動(dòng)應(yīng)用,會(huì)發(fā)現(xiàn)引用了dev的這份配置信息。
可以看出上面三個(gè)配置文件符合 application-{profile}.properties 格式,而在application.properties添加的spring.profiles.active=dev 中的dev正是上面配置文件中的 profile。根據(jù)具體環(huán)境進(jìn)行切換即刻。
用命令運(yùn)行jar包啟動(dòng)應(yīng)用的時(shí)候,可以指定相應(yīng)的配置.
java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
附:配置方式和優(yōu)先級(jí)
這些方式優(yōu)先級(jí)如下:
a. 命令行參數(shù)
b. 來(lái)自java:comp/env的JNDI屬性
c. Java系統(tǒng)屬性(System.getProperties())
d. 操作系統(tǒng)環(huán)境變量
e. RandomValuePropertySource配置的random.*屬性值
f. jar外部的application-{profile}.properties或application.yml(帶spring.profile)配置文件
g. jar內(nèi)部的application-{profile}.properties或application.yml(帶spring.profile)配置文件
h. jar外部的application.properties或application.yml(不帶spring.profile)配置文件
i. jar內(nèi)部的application.properties或application.yml(不帶spring.profile)配置文件
j. @Configuration注解類(lèi)上的@PropertySource
k. 通過(guò)SpringApplication.setDefaultProperties指定的默認(rèn)屬性
注:命令行參數(shù)這種jar包指定參數(shù)啟動(dòng)應(yīng)用的方式,可能是不安全的,我們可以設(shè)置禁止這種方式啟動(dòng)應(yīng)用,如下:
springApplication.setAddCommandLineProperties(false);
package com.sam.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
// SpringApplication.run(DemoApplication.class, args);
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
//禁止命令行設(shè)置參數(shù)
springApplication.setAddCommandLineProperties(false);
springApplication.run(args);
}
}
補(bǔ)充:
在spring boot 中配置除了支持 application.properties,還支持application.yml的配置方式,如下:
新建application.yml代替application.properties
server: port: 9999 com: sam: name: sam age: 11 desc: magical sam
注意:port: 9999 中間是有空格的,yml語(yǔ)法請(qǐng)參考:yml配置文件用法
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中@DateTimeFormat和@JsonFormat注解介紹
@DateTimeFormat和@JsonFormat都是處理時(shí)間格式化問(wèn)題的,把其他類(lèi)型轉(zhuǎn)換成自己需要的時(shí)間類(lèi)型,下面這篇文章主要給大家介紹了關(guān)于Java中@DateTimeFormat和@JsonFormat注解介紹的相關(guān)資料,需要的朋友可以參考下2022-11-11
Spring MVC接口防數(shù)據(jù)篡改和重復(fù)提交
這篇文章主要為大家詳細(xì)介紹了Spring MVC接口防數(shù)據(jù)篡改和重復(fù)提交,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
如何使用Springfox?Swagger實(shí)現(xiàn)API自動(dòng)生成單元測(cè)試
Springfox是一個(gè)使用Java語(yǔ)言開(kāi)發(fā)開(kāi)源的API Doc的框架,它的前身是swagger-springmvc,可以將我們的Controller中的方法以文檔的形式展現(xiàn),這篇文章主要介紹了如何使用Springfox?Swagger實(shí)現(xiàn)API自動(dòng)生成單元測(cè)試,感興趣的朋友跟隨小編一起看看吧2024-04-04
SpringBoot使用validation-api實(shí)現(xiàn)對(duì)枚舉類(lèi)參數(shù)校驗(yàn)的方法
這篇文章主要介紹了SpringBoot使用validation-api實(shí)現(xiàn)對(duì)枚舉類(lèi)參數(shù)校驗(yàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Java Listener監(jiān)聽(tīng)器使用規(guī)范詳細(xì)介紹
監(jiān)聽(tīng)器是一個(gè)專(zhuān)門(mén)用于對(duì)其他對(duì)象身上發(fā)生的事件或狀態(tài)改變進(jìn)行監(jiān)聽(tīng)和相應(yīng)處理的對(duì)象,當(dāng)被監(jiān)視的對(duì)象發(fā)生情況時(shí),立即采取相應(yīng)的行動(dòng)。監(jiān)聽(tīng)器其實(shí)就是一個(gè)實(shí)現(xiàn)特定接口的普通java程序,這個(gè)程序?qū)iT(mén)用于監(jiān)聽(tīng)另一個(gè)java對(duì)象的方法調(diào)用或?qū)傩愿淖?/div> 2023-01-01
在java中判斷兩個(gè)浮點(diǎn)型(float)數(shù)據(jù)是否相等的案例
這篇文章主要介紹了在java中判斷兩個(gè)浮點(diǎn)型(float)數(shù)據(jù)是否相等的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Java?Optional避免空指針異常的實(shí)現(xiàn)
空指針異常一直是困擾開(kāi)發(fā)者的常見(jiàn)問(wèn)題之一,本文主要介紹了Java?Optional避免空指針異常的實(shí)現(xiàn),幫助開(kāi)發(fā)者編寫(xiě)更健壯、可讀性更高的代碼,減少因空值處理不當(dāng)而引發(fā)的錯(cuò)誤,感興趣的可以了解一下2025-04-04最新評(píng)論

