Spring Boot配置特定屬性spring.profiles的方法

這篇文章主要介紹了Spring Boot配置特定屬性spring.profiles的方法,非常不錯,具有一定的參考借鑑價值,需要的朋友可以參考下

Spring Boot配置特定屬性spring.profiles

SpringBoot能使用application- {你的自定義profile名稱myProfileName} .properties模式添加任何你指定配置文件到其屬性文件。

要加載特定的配置文件屬性文件,我們可以使用命令行選項-Dspring.profiles.active = myProfileName。

缺省默認SpringBoot是加載application.properties,無需任何-Dspring.profile.active選項,或使用-Dspring.profiles.active = default來加載。默認屬性文件也可以命名爲application-default.properties

默認配置文件application.properties中指定的任何屬性將被你指定加載的配置文件中的的屬性覆蓋。

也可以在application.properties中指定激活配置文件。

spring.profiles.active=prod

比如你有三個配置文件:

src/main/resources/application.properties(默認的)

src/main/resources/application-dev.properties(你指定的dev)

src/main/resources/application-prod.properties(你指定的prod)

如果在application.properties中有:

spring.profiles.active=prod

那麼SpringBoot將加載application-prod.properties內容。

如果你在代碼中使用配置文件中的變量:

@Component
<b>public</b> <b>class</b> ClientBean {
 @Value(<font>"${app.window.width}"</font><font>)
 <b>private</b> <b>int</b> width;
 @Value(</font><font>"${app.window.height}"</font><font>)
 <b>private</b> <b>int</b> height;
</font>

如果application-prod.properties和application.properties都有app.window.width和app.window.height,那麼以prod中配置的值爲主。

spring.profile.include屬性

在application-prod.properties還可以加入

spring.profiles.include=throttling,db

這是無條件地添加活動配置文件(以逗號分隔)。此屬性添加的配置文件不會根據某些條件或命令行開關決定是否添加,而是始終無條件添加它們。

上述配置是就加載了:

src/main/resources/application-throttling.properties
src/main/resources/application-db.properties

這兩個配置文件中的內容。

總結

以上所述是小編給大家介紹的Spring Boot配置特定屬性spring.profiles,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對神馬文庫網站的支持!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章