SpringBoot?@Profile的使用
SpringBoot @Profile@Profile 可以和 @Bean 一起使用,
@Profile 可以和 @Bean 一起使用, 當(dāng)配置文件, 例如application.properties等, 添加了下列條目??
spring.profiles.active: profileName
或著
spring.profiles.include: profileName
加注了@Profile("profileName")
和@Bean
的方法就會在啟動時被執(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 了 這個方法上面的 @Profile("ProfileName") 中的 ProfileName (區(qū)分大小寫) 這個方法就會被執(zhí)行 例如: spring.profiles.active= ProfileName 或 spring.profiles.include= ProfileName 控制臺就會輸出這條信息\n """ + "????????????????????????????????????????????????\n\n\n "); return "hello"; } }
也可以同時匹配多個名稱@Profile({"name1","name2","name3"})
import org.springframework.context.annotation.*; import org.springframework.stereotype.Component; @Component @Profile({"是否支持@符號","啟用ImportProfile類","可以使用多個名稱", "可以使用@@@@@@符號", "可以支持中文" , "類上的@Profile相當(dāng)于總開關(guān)" , "類上的@Profile可以沒有"}) public class ImportProfile { @Profile({"ProfileName", "可以用字符串?dāng)?shù)組匹配多個", "名稱2", "名稱3" }) @Bean public Object beanMethod666() { System.out.println( """ \n\n\n???????????????????????????????????????? 當(dāng) active 或 include 了 這個方法上面的 ProfileName (區(qū)分大小寫) @Profile({"ProfileName", "可以用字符串?dāng)?shù)組匹配多個", "名稱2", "名稱3" }) 中的其中一個名稱, 這個方法就會被執(zhí)行 例如: spring.profiles.active: 啟用ImportProfile類 或 spring.profiles.include: 可以用字符串?dāng)?shù)組匹配多個 控制臺就會輸出這條信息 ????????????????????????????????????????????????\n\n\n """); return new ImportProfile(){}; //返回一個匿名內(nèi)部類意思意思 } }
中文.properties中會轉(zhuǎn)義顯示
到此這篇關(guān)于SpringBoot @Profile的使用的文章就介紹到這了,更多相關(guān)SpringBoot @Profile內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中l(wèi)ambda表達式實現(xiàn)aop切面功能
本文主要介紹了Java中l(wèi)ambda表達式實現(xiàn)aop切面功能,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Maven項目外部jar包導(dǎo)入的實現(xiàn)示例
在Maven項目里,我們經(jīng)常需要導(dǎo)入jar包依賴,本文主要介紹了Maven項目外部jar包導(dǎo)入的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-08-08Java后端學(xué)習(xí)精華之TCP通信傳輸協(xié)議詳解
TCP/IP是一種面向連接的、可靠的、基于字節(jié)流的傳輸層通信協(xié)議,它會保證數(shù)據(jù)不丟包、不亂序。TCP全名是Transmission Control Protocol,它是位于網(wǎng)絡(luò)OSI模型中的第四層2021-09-09如何將JSP/Servlet項目轉(zhuǎn)換為Spring Boot項目
這篇文章主要介紹了如何將JSP/Servlet項目轉(zhuǎn)換為Spring Boot項目,幫助大家更好的利用springboot進行網(wǎng)絡(luò)編程,感興趣的朋友可以了解下2020-10-10詳解SpringBoot?統(tǒng)一后端返回格式的方法
今天我們來聊一聊在基于SpringBoot前后端分離開發(fā)模式下,如何友好的返回統(tǒng)一的標(biāo)準(zhǔn)格式以及如何優(yōu)雅的處理全局異常,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2022-05-05