Spring Profiles使用方法詳解
Spring Profiles
今天學(xué)習(xí)下,Spring的核心功能之一 profiles,該特性允許開(kāi)發(fā)者將beans映射到不同的環(huán)境中,如dev、test、prod。開(kāi)發(fā)者啟動(dòng)服務(wù)時(shí),可以根據(jù)自身需要在不同的環(huán)境中激活不同的配置。
bean使用profile注解
先來(lái)學(xué)習(xí)一個(gè)最簡(jiǎn)單profle的使用方式,學(xué)習(xí)如何讓bean屬于特定的環(huán)境。假設(shè)一個(gè)場(chǎng)景:一個(gè)普通的bean,只在開(kāi)發(fā)期間有效,其他環(huán)境無(wú)效。
@Component @Profile("dev") public class DevDatasourceConfig{ }
如上述代碼,只需要在聲明bean時(shí),配合@Profile注解,并指定特定的環(huán)境即可。根據(jù)上面的場(chǎng)景,反過(guò)來(lái)看:假設(shè)一個(gè)bean除了在開(kāi)發(fā)期間無(wú)效,在其他環(huán)境(如test、prod)有效。@Profile支持NOT操作,只需要在前面加上 ! 符號(hào)。例如 !dev, 就可以將dev環(huán)境排除。
@Component @Profile("!dev") // @Profile(value={"dev & local"}) public class DevDatasourceConfig{ }
XML聲明profile
在XML配置文件中也可以配置profiles屬性, 標(biāo)簽中定義了一個(gè) profile 屬性,多個(gè)屬性值可以使用逗號(hào)分隔
<beans profile="dev"> <bean id="devDatasourceConfig" class="org.baeldung.profiles.DevDatasourceConfig" /> </beans>
設(shè)置profile
可以通過(guò)多種方式設(shè)置profile向容器中注冊(cè)bean。
WebApplicationInitializer 接口
web環(huán)境中,可以通過(guò)實(shí)現(xiàn)WebApplicationInitializer接口配置ServletContext上下文。
@Configuration public class MyWebApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.setInitParameter( "spring.profiles.active", "dev"); } }
ConfigurableEnvironment 接口
通過(guò)ConfigurableEnvironment接口直接設(shè)置profile
@Autowired private ConfigurableEnvironment env; ... env.setActiveProfiles("someProfile");
Web.xml
web開(kāi)發(fā)者可以在web.xml中使用context param激活profile屬性
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/app-config.xml</param-value> </context-param> <context-param> <param-name>spring.profiles.active</param-name> <param-value>dev</param-value> </context-param>
JVM 設(shè)置
profiles屬性也可以通過(guò)JVM系統(tǒng)參數(shù)設(shè)置,并在應(yīng)用啟動(dòng)時(shí)激活相關(guān)屬性
-Dspring.profiles.active=dev
系統(tǒng)環(huán)境變量
Unix系統(tǒng)中,profiles可以通過(guò)聲明系統(tǒng)變量來(lái)激活
export spring_profiles_active=dev
Maven設(shè)置
Spring profiles屬性通過(guò)maven配置文件聲明激活。
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <spring.profiles.active>dev</spring.profiles.active> </properties> </profile> <profile> <id>prod</id> <properties> <spring.profiles.active>prod</spring.profiles.active> </properties> </profile> </profiles>
在編譯打包時(shí),通過(guò)以下動(dòng)態(tài)參數(shù)傳遞,直接指定profile屬性,開(kāi)發(fā)不需要改動(dòng)任何代碼。這種方式在實(shí)際開(kāi)發(fā)中經(jīng)常使用,編譯打包完成后,直接交付給運(yùn)維團(tuán)隊(duì)
mvn clean package -Pprod
Profiles In Test
開(kāi)發(fā)測(cè)試時(shí),使用@ActiveProfile注解指定需要激活的profile。
@ActiveProfiles("dev")
目前為止,已知多種方式激活profile屬性,它們的優(yōu)先級(jí),從高到低分別為:
- Context parameter in web.xml
- WebApplicationInitializer
- JVM System parameter
- Environment variable
- Maven profile
默認(rèn)Profile
如果沒(méi)有指定profile,Spring激活默認(rèn)的profile - default, 可以在屬性文件值修改 spring.profiles.default 的值,從而修改默認(rèn)profile的名字
spring.profiles.default=none
獲取生效的Profiles
Spring通過(guò)@Profile注解激活/禁止beans, 但是開(kāi)發(fā)者希望獲取生效的Profiles列表。有兩種方式可以實(shí)現(xiàn):
- 使用 Environment 對(duì)象
- 獲取spring.profiles.active屬性值
使用Environment
通過(guò)注入Environment bean獲取激活的profiles
public class ProfileManager { @Autowired private Environment environment; public void getActiveProfiles() { for (String profileName : environment.getActiveProfiles()) { System.out.println("Currently active profile - " + profileName); } } }
使用spring.profiles.active
此外,可以通過(guò)注入spring.profiles.active屬性值來(lái)獲取有效的profiles。
public class ProfileManager { //如果配置多個(gè)屬性,則覆蓋get方法 迭代出每一個(gè)有效的profile @Value("${spring.profiles.active}") private String activeProfiles; public String getActiveProfiles() { for (String profileName : activeProfiles.split(",")) { System.out.println("Currently active profile - " + profileName); } } }
但是,如果應(yīng)用中沒(méi)有profile,上述代碼,由于缺少配置將會(huì)導(dǎo)致應(yīng)用啟動(dòng)失敗,為了避免這種情況,可以定義個(gè) kong的字符串作為默認(rèn)值。
//@Value("${spring.profiles.active}") @Value("${spring.profiles.active:}") private String activeProfile;
SpringBoot Profiles
Spring Boot 支持以上所有的功能,并增加了一些額外的特性。
設(shè)置Profiles
在Spring Boot 默認(rèn)的配置文件 - application.properties 激活
spring.profiles.active=dev
通過(guò)啟動(dòng)類設(shè)置profile
//setAdditionalProfiles 不是靜態(tài)方法,在實(shí)際使用中需要注意 SpringApplication.setAdditionalProfiles("dev");
使用spring-boot-maven-plugin插件
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <profiles> <profile>dev</profile> </profiles> </configuration> </plugin> ... </plugins>
執(zhí)行maven命令
mvn spring-boot:run
Profile-specific Properties Files
Spring Boot 核心特性之一是定義了基于profile的配置文件解析規(guī)則。配置文件必須以-{profile}.properties格式命名。Spring Boot自動(dòng)加載解析application.properties文件,并根據(jù)profile指定,加載特定的 -{profile}.properties 文件。
例如,需要配置開(kāi)發(fā)/生產(chǎn)兩種數(shù)據(jù)源,名稱分別為application-dev.properties、application-production.properties。
application-production.properties使用MYSQL數(shù)據(jù)源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=root
spring.datasource.password=root
application-dev.properties 使用內(nèi)存數(shù)據(jù)庫(kù)
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
單文件配置
為了簡(jiǎn)化不同環(huán)境的配置,開(kāi)發(fā)者可以在同一個(gè)文件中定義所有屬性,并使用分隔符來(lái)指定配置文件。
my.prop=used-always-in-all-profiles
#---
spring.config.activate.on-profile=dev
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=root
spring.datasource.password=root
#---
spring.config.activate.on-profile=production
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
Profile Group
Spring Boot 2.4 添加的另一個(gè)特性 - Profile Group, 顧明思義,它允許開(kāi)發(fā)者將類似的配置文件分組放置在一起。
假設(shè)一個(gè)場(chǎng)景:需要為生產(chǎn)環(huán)境提供多個(gè)配置概要文件,例如,生產(chǎn)環(huán)境中的數(shù)據(jù)庫(kù)proddb、調(diào)度程序的prodquartz。
spring.profiles.group.production=proddb,prodquartz
到此這篇關(guān)于Spring Profiles使用方法詳解的文章就介紹到這了,更多相關(guān)Spring Profiles內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基本數(shù)據(jù)類型族譜與易錯(cuò)點(diǎn)梳理解析
Java有八大基本類型,很多同學(xué)只對(duì)經(jīng)常使用的int類型比較了解。有的同學(xué)是剛從C語(yǔ)言轉(zhuǎn)入Java學(xué)習(xí),誤以為兩者的基本數(shù)據(jù)類型完全相同,這也是大錯(cuò)特錯(cuò)的。今天這本Java基本數(shù)據(jù)類型全解析大字典,可以幫助你直接通過(guò)目錄找到你想要了解某一種基本數(shù)據(jù)類型2022-01-01Java數(shù)據(jù)結(jié)構(gòu)之鏈表的概念及結(jié)構(gòu)
這篇文章主要介紹了數(shù)據(jù)鏈表的概念及結(jié)構(gòu),鏈表是一種物理存儲(chǔ)結(jié)構(gòu)上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過(guò)鏈表中的指針鏈接次序?qū)崿F(xiàn)的。想進(jìn)一步了解的同學(xué),可以參考閱讀本文2023-04-04Springboot使用filter對(duì)response內(nèi)容進(jìn)行加密方式
這篇文章主要介紹了Springboot使用filter對(duì)response內(nèi)容進(jìn)行加密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03解決IDEA中pom.xml文件變?yōu)榛疑膯?wèn)題
這篇文章主要給大家介紹了如何解決IDEA中pom.xml文件變?yōu)榛疑膯?wèn)題,文中通過(guò)圖文結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-12-12Java RabbitMQ 中的消息長(zhǎng)期不消費(fèi)會(huì)過(guò)期嗎
RabbitMQ支持消息的過(guò)期時(shí)間,在消息發(fā)送時(shí)可以進(jìn)行指定。 RabbitMQ支持隊(duì)列的過(guò)期時(shí)間,從消息入隊(duì)列開(kāi)始計(jì)算,只要超過(guò)了隊(duì)列的超時(shí)時(shí)間配置,那么消息會(huì)自動(dòng)的清除2021-09-09使用hutool工具進(jìn)行導(dǎo)入導(dǎo)出excel表格
如何在后臺(tái)添加導(dǎo)入導(dǎo)出表格的功能呢,本期的文章將會(huì)帶領(lǐng)小伙伴們一起實(shí)現(xiàn)此功能,文中有詳細(xì)的代碼示例和圖文介紹,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10JVM系列之:再談java中的safepoint說(shuō)明
這篇文章主要介紹了JVM系列之:再談java中的safepoint說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09java抓取鼠標(biāo)事件和鼠標(biāo)滾輪事件示例
這篇文章主要介紹了java抓取鼠標(biāo)事件和鼠標(biāo)滾輪事件示例,需要的朋友可以參考下2014-05-05使用spring boot通過(guò)自定義注解打印所需日志
這篇文章主要介紹了使用spring boot通過(guò)自定義注解打印所需日志的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java設(shè)計(jì)模式之java狀態(tài)模式詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之狀態(tài)模式定義與用法,結(jié)合具體實(shí)例形式詳細(xì)分析了Java狀態(tài)模式的概念、原理、定義及相關(guān)操作技巧,需要的朋友可以參考下2021-09-09