SpringBoot中多環(huán)境配置和@Profile注解示例詳解
一、使用@Profile
1.1、@Profile修飾類
開發(fā)環(huán)境
package com.example.demo.config; import com.example.demo.entity.AppData; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @Profile("development") public class DevelopmentConfig { @Bean public AppData getAppData() { AppData appData = new AppData(); appData.setEnvironmentName("development"); return appData; } }
正式環(huán)境
package com.example.demo.config; import com.example.demo.entity.AppData; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @Profile("production") public class ProductionConfig { @Bean public AppData getAppData() { AppData appData = new AppData(); appData.setEnvironmentName("production"); return appData; } }
1.2、@Profile修飾方法
package com.example.demo.config; import com.example.demo.entity.AppData; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration public class AppConfig { // 開發(fā)環(huán)境 @Bean("appConfigData") @Profile("development") public AppData getDevelopmentAppData() { AppData appData = new AppData(); appData.setEnvironmentName("app development"); return appData; } // 正式環(huán)境 @Bean("appConfigData") @Profile("production") public AppData getProductionAppData() { AppData appData = new AppData(); appData.setEnvironmentName("app production"); return appData; } }
1.3、@Profile修飾注解
1、定義注解
開發(fā)環(huán)境
package com.example.demo.annotation; import org.springframework.context.annotation.Profile; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Profile("development") public @interface Development { }
正式環(huán)境
package com.example.demo.annotation; import org.springframework.context.annotation.Profile; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Profile("production") public @interface Production { }
2、使用注解
package com.example.demo.config; import com.example.demo.annotation.Development; import com.example.demo.annotation.Production; import com.example.demo.entity.AppData; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { // 開發(fā)環(huán)境 @Bean("appConfigData") @Development public AppData getDevelopmentAppData() { AppData appData = new AppData(); appData.setEnvironmentName("app development"); return appData; } // 正式環(huán)境 @Bean("appConfigData") @Production public AppData getProductionAppData() { AppData appData = new AppData(); appData.setEnvironmentName("app production"); return appData; } }
二、激活@Profile
2.1、配置文件方式激活@Profile
application.properties
spring.profiles.active=production
application.yml
spring: profiles: active: production
2.2、命令行方式激活@Profile
java -jar target/demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=production
三、多Profile資源文件
配置文件
# 公共配置 application.properties # development application-development.properties # production application-production.properties
application.properties
# 激活配置文件 spring.profiles.active=production
application-development.properties
server.port=8081
application-production.properties
server.port=8082
完整代碼 https://github.com/mouday/spring-boot-demo/tree/master/SpringBoot-Profile
參考
Springboot中的@Profile注解
到此這篇關(guān)于SpringBoot中多環(huán)境配置和@Profile注解的文章就介紹到這了,更多相關(guān)SpringBoot 多環(huán)境配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Java的Hibernate框架中對數(shù)據(jù)庫數(shù)據(jù)進行查詢操作
這篇文章主要介紹了Java的Hibernate框架中對數(shù)據(jù)庫數(shù)據(jù)進行查詢操作的方法,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12Java二分法查找_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Java二分法查找的相關(guān)資料,需要的朋友可以參考下2017-04-04SpringCloud引入feign失敗或找不到@EnableFeignClients注解問題
這篇文章主要介紹了SpringCloud引入feign失敗或找不到@EnableFeignClients注解問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Java+MySQL實現(xiàn)設(shè)計優(yōu)惠券系統(tǒng)
這篇文章主要介紹了Java+MySQL實現(xiàn)設(shè)計優(yōu)惠券系統(tǒng),文章基于Java與MySQL的相關(guān)資料展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05