其書寫風格為小數點間隔級別書寫全路徑。這個老**裡面見的比較多。
示例如下:
server.port=
8080
spring.datasource.url=jdbc:mysql:
//localhost:
3306
/demo
spring.datasource.username=root
spring.datasource.password=root
# 演示內容
demo.username=test
demo.password=test
server:
port:
8080
spring:
datasource:
url: jdbc:mysql:
//localhost:
3306
/demo
username: root
password: root
# 演示內容
demo:
username: test
password: test
如果是要讀取單個或幾個配置值的資訊,可以直接在業務bean中引入這個成員變數,並加上@value註解宣告。
// 其他包
import org.springframework.beans.factory.annotation.value;
@component
public
class
readconfi**aluedemo")
private string username;
@value
("$"
)private string password;
// 業務**
}
如果需要讀取的配置檔案很多,或則是一組相關的配置檔案,希望在系統組裝起來復用,那麼我們可以採用構建配置bean的方式。
這是為了第二步配置bean的時候能掃瞄到配置檔案資訊
org.springframework.boot<
/groupid>
spring-boot-configuration-processor<
/artifactid>
true
<
/optional>
<
/dependency>
通過configurationproperties的prefix字首屬性,我們可以指定一組配置值,注意屬性名要和配置檔案一致,類名無所謂。
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.stereotype.component;
@component
@configurationproperties
(prefix=
"demo"
)public
class
democonfig
public string getpassword()
}
**需要這一組配置檔案,就通過@resource或則@autowired註解自動注入即可。
注意:注入配置bean的類,本身必須也是spring管理下的乙個bean,否則會注入null值。這種情況在一些提供靜態方法的工具類上可能出現。
@service
public
class
demoserviceimpl
}
# 當前啟用dev配置檔案
spring:
profiles:
active: dev
@profile
("dev"
)// 僅在dev環境下生效
@component
@configurationproperties
(prefix=
"demo"
)public
class
democonfig
@profile
("!prod"
)// prod環境下不生效
@component
@configurationproperties
(prefix=
"demo"
)public
class
democonfig
Spring Boot讀取配置檔案與配置檔案優先順序
spring boot讀取配置檔案 package com.ivan.config.controller import org.springframework.beans.factory.annotation.autowired import org.springframework.core.env...
5分鐘學會springboot讀取配置檔案
本文是基於hadoop ha基礎搭建下storm集群的搭建過程 之前文章有搭建過程 搭建過程超級簡單如下 2 安裝zookeeper集群 準備三颱機器,分別為weekend01,weekend02,weekend03,並在這三颱機器上安裝zookeeper,我們現在weekend01上安裝,然後zo...
spring boot 配置檔案讀取
如圖所示,可以重新賦值予以覆蓋。mail setting 設定郵箱主機 email.host smtp.163.com email.port 25 設定使用者名稱 email.from xx xx.com 設定密碼 email.frompassword 設定是否需要認證,如果為true,那麼使用者名...