gpt4 book ai didi

java - 如果未设置 Activity 配置文件,则退出

转载 作者:行者123 更新时间:2023-11-30 02:18:15 26 4
gpt4 key购买 nike

如果没有 Activity 配置文件,如何在执行任何操作(尤其是 Bean 创建)之前退出应用程序?

我已经使用这样的配置完成了此操作:

@Profile("default")
@Configuration
public class NoActiveProfileKiller {
public NoActiveProfileKiller() {
throw new UnsupportedOperationException("No active profile is selected");
}
}

但这至少执行 JPA 操作。

最佳答案

我已经通过使用监听器的下一个方法完成了此操作:

@SpringBootApplication
public class MyApplication implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(MyApplication.class);

public static void main(String[] args) {
new SpringApplicationBuilder()
.listeners(new MyApplication())
.sources(MyApplication.class)
.run(args);
}

@Override
public void onApplicationEvent(final ApplicationEnvironmentPreparedEvent event) {
if (event.getEnvironment().getActiveProfiles().length == 0) {
LOGGER.error("No active profile is set. Will terminate");
System.exit(1);
}
}
}

关于java - 如果未设置 Activity 配置文件,则退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47636583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com