gpt4 book ai didi

spring-boot - 仅为嵌入式 tomcat 或嵌入式服务器创建 Bean

转载 作者:行者123 更新时间:2023-12-04 00:12:43 26 4
gpt4 key购买 nike

如果应用程序在嵌入式容器中运行,我希望创建 @Bean。如果应用程序在外部 tomcat 上运行,则不应创建该 bean。有什么方法可以创建@Conditional 注释以仅当应用程序在嵌入式 tomcat 中运行时才创建 bean。

最佳答案

您可以使用仅在使用嵌入式容器时启用的 Spring 配置文件,而不是使用自定义条件。当您将 Spring Boot 应用程序部署到 Tomcat 时,它的 main 方法不会运行,这使它成为启用您只想在嵌入式情况下处于事件状态的配置文件的好地方。

像这样的东西:

@SpringBootApplication
public class So34924050Application extends SpringBootServletInitializer {

@Bean
@Profile("embedded")
public EmbeddedOnlyBean embeddedOnlyBean() {
return new EmbeddedOnlyBean();
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(So34924050Application.class);
}

public static void main(String[] args) {
new SpringApplicationBuilder(So34924050Application.class).profiles("embedded").run(args);
}
}

关于spring-boot - 仅为嵌入式 tomcat 或嵌入式服务器创建 Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34924050/

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