Spring Profiles使用方法詳解
Spring Profiles
今天學(xué)習(xí)下,Spring的核心功能之一 profiles,該特性允許開發(fā)者將beans映射到不同的環(huán)境中,如dev、test、prod。開發(fā)者啟動服務(wù)時,可以根據(jù)自身需要在不同的環(huán)境中激活不同的配置。
bean使用profile注解
先來學(xué)習(xí)一個最簡單profle的使用方式,學(xué)習(xí)如何讓bean屬于特定的環(huán)境。假設(shè)一個場景:一個普通的bean,只在開發(fā)期間有效,其他環(huán)境無效。
@Component @Profile("dev") public class DevDatasourceConfig{ }
如上述代碼,只需要在聲明bean時,配合@Profile注解,并指定特定的環(huán)境即可。根據(jù)上面的場景,反過來看:假設(shè)一個bean除了在開發(fā)期間無效,在其他環(huán)境(如test、prod)有效。@Profile支持NOT操作,只需要在前面加上 ! 符號。例如 !dev, 就可以將dev環(huán)境排除。
@Component @Profile("!dev") // @Profile(value={"dev & local"}) public class DevDatasourceConfig{ }
XML聲明profile
在XML配置文件中也可以配置profiles屬性, 標(biāo)簽中定義了一個 profile 屬性,多個屬性值可以使用逗號分隔
<beans profile="dev"> <bean id="devDatasourceConfig" class="org.baeldung.profiles.DevDatasourceConfig" /> </beans>
設(shè)置profile
可以通過多種方式設(shè)置profile向容器中注冊bean。
WebApplicationInitializer 接口
web環(huán)境中,可以通過實現(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 接口
通過ConfigurableEnvironment接口直接設(shè)置profile
@Autowired private ConfigurableEnvironment env; ... env.setActiveProfiles("someProfile");
Web.xml
web開發(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屬性也可以通過JVM系統(tǒng)參數(shù)設(shè)置,并在應(yīng)用啟動時激活相關(guān)屬性
-Dspring.profiles.active=dev
系統(tǒng)環(huán)境變量
Unix系統(tǒng)中,profiles可以通過聲明系統(tǒng)變量來激活
export spring_profiles_active=dev
Maven設(shè)置
Spring profiles屬性通過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>
在編譯打包時,通過以下動態(tài)參數(shù)傳遞,直接指定profile屬性,開發(fā)不需要改動任何代碼。這種方式在實際開發(fā)中經(jīng)常使用,編譯打包完成后,直接交付給運維團隊
mvn clean package -Pprod
Profiles In Test
開發(fā)測試時,使用@ActiveProfile注解指定需要激活的profile。
@ActiveProfiles("dev")
目前為止,已知多種方式激活profile屬性,它們的優(yōu)先級,從高到低分別為:
- Context parameter in web.xml
- WebApplicationInitializer
- JVM System parameter
- Environment variable
- Maven profile
默認Profile
如果沒有指定profile,Spring激活默認的profile - default, 可以在屬性文件值修改 spring.profiles.default 的值,從而修改默認profile的名字
spring.profiles.default=none
獲取生效的Profiles
Spring通過@Profile注解激活/禁止beans, 但是開發(fā)者希望獲取生效的Profiles列表。有兩種方式可以實現(xiàn):
- 使用 Environment 對象
- 獲取spring.profiles.active屬性值
使用Environment
通過注入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
此外,可以通過注入spring.profiles.active屬性值來獲取有效的profiles。
public class ProfileManager { //如果配置多個屬性,則覆蓋get方法 迭代出每一個有效的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)用中沒有profile,上述代碼,由于缺少配置將會導(dǎo)致應(yīng)用啟動失敗,為了避免這種情況,可以定義個 kong的字符串作為默認值。
//@Value("${spring.profiles.active}") @Value("${spring.profiles.active:}") private String activeProfile;
SpringBoot Profiles
Spring Boot 支持以上所有的功能,并增加了一些額外的特性。
設(shè)置Profiles
在Spring Boot 默認的配置文件 - application.properties 激活
spring.profiles.active=dev
通過啟動類設(shè)置profile
//setAdditionalProfiles 不是靜態(tài)方法,在實際使用中需要注意 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自動加載解析application.properties文件,并根據(jù)profile指定,加載特定的 -{profile}.properties 文件。
例如,需要配置開發(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ù)庫
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
單文件配置
為了簡化不同環(huán)境的配置,開發(fā)者可以在同一個文件中定義所有屬性,并使用分隔符來指定配置文件。
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 添加的另一個特性 - Profile Group, 顧明思義,它允許開發(fā)者將類似的配置文件分組放置在一起。
假設(shè)一個場景:需要為生產(chǎn)環(huán)境提供多個配置概要文件,例如,生產(chǎn)環(huán)境中的數(shù)據(jù)庫proddb、調(diào)度程序的prodquartz。
spring.profiles.group.production=proddb,prodquartz
到此這篇關(guān)于Spring Profiles使用方法詳解的文章就介紹到這了,更多相關(guān)Spring Profiles內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之鏈表的概念及結(jié)構(gòu)
這篇文章主要介紹了數(shù)據(jù)鏈表的概念及結(jié)構(gòu),鏈表是一種物理存儲結(jié)構(gòu)上非連續(xù)、非順序的存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。想進一步了解的同學(xué),可以參考閱讀本文2023-04-04Springboot使用filter對response內(nèi)容進行加密方式
這篇文章主要介紹了Springboot使用filter對response內(nèi)容進行加密方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03使用hutool工具進行導(dǎo)入導(dǎo)出excel表格
如何在后臺添加導(dǎo)入導(dǎo)出表格的功能呢,本期的文章將會帶領(lǐng)小伙伴們一起實現(xiàn)此功能,文中有詳細的代碼示例和圖文介紹,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10java抓取鼠標(biāo)事件和鼠標(biāo)滾輪事件示例
這篇文章主要介紹了java抓取鼠標(biāo)事件和鼠標(biāo)滾輪事件示例,需要的朋友可以參考下2014-05-05