SpringBoot?@Profile的使用
SpringBoot @Profile@Profile 可以和 @Bean 一起使用,
@Profile 可以和 @Bean 一起使用, 當(dāng)配置文件, 例如application.properties等, 添加了下列條目??
spring.profiles.active: profileName
或著
spring.profiles.include: profileName
加注了@Profile("profileName")和@Bean的方法就會(huì)在啟動(dòng)時(shí)被執(zhí)行, 當(dāng)然,類要加@Configuration,甚至@Component就行,當(dāng)然,@Bean應(yīng)該配@Configuration
例如
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;
@Component
public class ImportProfile {
@Profile("ProfileName")
@Bean
public Object beanMethod666() {
System.out.println("\n\n\n????????????????????????????????????????"+
"""
\n
當(dāng) active 或 include 了 這個(gè)方法上面的 @Profile("ProfileName") 中的 ProfileName (區(qū)分大小寫)
這個(gè)方法就會(huì)被執(zhí)行
例如:
spring.profiles.active= ProfileName
或
spring.profiles.include= ProfileName
控制臺(tái)就會(huì)輸出這條信息\n
"""
+ "????????????????????????????????????????????????\n\n\n ");
return "hello";
}
}
也可以同時(shí)匹配多個(gè)名稱@Profile({"name1","name2","name3"})
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Profile({"是否支持@符號(hào)","啟用ImportProfile類","可以使用多個(gè)名稱", "可以使用@@@@@@符號(hào)", "可以支持中文"
, "類上的@Profile相當(dāng)于總開關(guān)" , "類上的@Profile可以沒有"})
public class ImportProfile {
@Profile({"ProfileName", "可以用字符串?dāng)?shù)組匹配多個(gè)", "名稱2", "名稱3" })
@Bean
public Object beanMethod666() {
System.out.println(
"""
\n\n\n????????????????????????????????????????
當(dāng) active 或 include 了 這個(gè)方法上面的 ProfileName (區(qū)分大小寫)
@Profile({"ProfileName", "可以用字符串?dāng)?shù)組匹配多個(gè)", "名稱2", "名稱3" })
中的其中一個(gè)名稱, 這個(gè)方法就會(huì)被執(zhí)行
例如:
spring.profiles.active: 啟用ImportProfile類
或
spring.profiles.include: 可以用字符串?dāng)?shù)組匹配多個(gè)
控制臺(tái)就會(huì)輸出這條信息
????????????????????????????????????????????????\n\n\n
""");
return new ImportProfile(){}; //返回一個(gè)匿名內(nèi)部類意思意思
}
}中文.properties中會(huì)轉(zhuǎn)義顯示



到此這篇關(guān)于SpringBoot @Profile的使用的文章就介紹到這了,更多相關(guān)SpringBoot @Profile內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
高可用架構(gòu)etcd選主故障主備秒級(jí)切換實(shí)現(xiàn)
這篇文章主要為大家介紹了高可用架構(gòu)etcd選主故障主備秒級(jí)切換的實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-02-02
Java開發(fā)框架spring實(shí)現(xiàn)自定義緩存標(biāo)簽
這篇文章主要介紹了Java開發(fā)框架spring實(shí)現(xiàn)自定義緩存標(biāo)簽的詳細(xì)代碼,感興趣的小伙伴們可以參考一下2015-12-12
使用Mybatis對(duì)數(shù)據(jù)庫進(jìn)行單表操作的實(shí)現(xiàn)示例
這篇文章主要介紹了使用Mybatis對(duì)數(shù)據(jù)庫進(jìn)行單表操作的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java servlet執(zhí)行流程代碼實(shí)例
這篇文章主要介紹了Java servlet執(zhí)行流程代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
java實(shí)現(xiàn)裝飾器模式(Decorator Pattern)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)裝飾器模式Decorator Pattern,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Spark學(xué)習(xí)筆記Spark Streaming的使用
這篇文章主要介紹了Spark學(xué)習(xí)筆記Spark Streaming的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

