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)類(lèi)設(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ù)源,名稱(chēng)分別為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ā)者將類(lèi)似的配置文件分組放置在一起。
假設(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中String類(lèi)的substring()字符串截取最后一個(gè)字符
Java中的String是不可變的類(lèi)型,因此substring()方法并不會(huì)改變?cè)址?而是返回了一個(gè)新的字符串,這篇文章主要介紹了如何利用java中String類(lèi)的substring()字符串截取最后一個(gè)字符,需要的朋友可以參考下2023-11-11
詳解Spring Data Jpa 模糊查詢(xún)的正確用法
本篇文章主要介紹了詳解Spring Data Jpa 模糊查詢(xún)的正確用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Nacos docker單機(jī)模式部署實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了Nacos docker單機(jī)模式部署實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot集成企業(yè)微信開(kāi)發(fā)的實(shí)現(xiàn)
本文將詳細(xì)介紹如何使用?Spring?Boot?集成企業(yè)微信開(kāi)發(fā),通過(guò)企業(yè)微信?API?可以實(shí)現(xiàn)企業(yè)內(nèi)部的一些自動(dòng)化業(yè)務(wù)流程,提高工作效率,感興趣的可以了解一下2023-07-07
Java的JSON格式轉(zhuǎn)換庫(kù)GSON的初步使用筆記
GSON是Google開(kāi)發(fā)并在在GitHub上開(kāi)源的Java對(duì)象與JSON互轉(zhuǎn)功能類(lèi)庫(kù),在Android開(kāi)發(fā)者中也大受歡迎,這里我們就來(lái)看一下Java的JSON格式轉(zhuǎn)換庫(kù)GSON的初步使用筆記:2016-06-06

