代碼示例
/*** 啟動監聽器*/
@Component
public class AppListener implements ApplicationListener<ApplicationReadyEvent> {@Overridepublic void onApplicationEvent(ApplicationReadyEvent event) {// 獲取當前的環境,如果是test,則直接返回ConfigurableApplicationContext applicationContext = event.getApplicationContext();ConfigurableEnvironment environment = applicationContext.getEnvironment();String[] activeProfiles = environment.getActiveProfiles();for (String activeProfile : activeProfiles) {if ("test".equals(activeProfile)) {return;}}}
}
參考
- springboot普通類獲取當前環境,判斷當前是否為開發環境,手動獲取bean,手動獲取配置值,獲取當前請求