代码示例

/**
 * 启动监听器
 */
@Component
public class AppListener implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public 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,手动获取配置值,获取当前请求