Spring Boot 使用配置文件启用/禁用嵌入式 tomcat
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32078015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot enable/disable embedded tomcat with profile
提问by barcom
I'm writing a Spring Boot application that uses one of several @Configurationclasses depending on which @Profileis set in the application.propertiesfile.
我正在编写一个 Spring Boot 应用程序,它使用几个@Configuration类之一,具体取决于文件中@Profile设置的类application.properties。
One of those Configuration classes uses a REST interface, and therefore I'm including spring-boot-starter-webas a dependency.
其中一个 Configuration 类使用 REST 接口,因此我将其spring-boot-starter-web作为依赖项包含在内。
This starts up an embedded Tomcat instance, which is fine.
这将启动一个嵌入式 Tomcat 实例,这很好。
The problem is, the other profiles don't need an embedded server (e.g. I'm using JMS to handle incoming messages instead of REST).
问题是,其他配置文件不需要嵌入式服务器(例如,我使用 JMS 来处理传入消息而不是 REST)。
Is there any way to stop the @SpringBootApplicationfrom starting up Tomcat by default, and only using it for the REST Configuration class?
E.g., by annotating that class with @EnableWebMVC
有什么方法可以阻止@SpringBootApplication默认情况下启动 Tomcat,并且仅将其用于 REST 配置类?例如,通过注释该类@EnableWebMVC
Here's an example of my @Configurationclasses:
这是我的@Configuration课程的一个例子:
REST:
休息:
@Profile({"REST"})
@Configuration
@EnableWebMvc
public class HttpConfiguration{
.
.
.
}
JMS:
管理系统:
@Profile({"JMS"})
@Configuration
@EnableJms
public class JMSConfiguration{
.
.
.
}
Thanks
谢谢
回答by hzpz
Use
用
@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,
WebMvcAutoConfiguration.class})
to exclude Spring Boot's auto-configuration for embedded servlet containers. Additionally, you need to set the following property for the non-REST cases, so that Spring Boot won't try to start a WebApplicationContext(which needs a servlet container):
排除 Spring Boot 对嵌入式 servlet 容器的自动配置。此外,您需要为非 REST 情况设置以下属性,以便 Spring Boot 不会尝试启动一个WebApplicationContext(需要一个 servlet 容器):
spring.main.web-environment=false
Then enable the embedded Tomcat in your REST profile by importing EmbeddedServletContainerAutoConfiguration.class(this delays the autoconfiguration until after the REST profile has been loaded:
然后通过导入在您的 REST 配置文件中启用嵌入式 Tomcat EmbeddedServletContainerAutoConfiguration.class(这会延迟自动配置,直到加载 REST 配置文件之后:
@Profile({"REST"})
@Configuration
@Import(EmbeddedServletContainerAutoConfiguration.class)
public class HttpConfiguration {
// ...
}
If you are using any EmbeddedServletContainerCustomizers, you also need to import EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar.class.
如果您正在使用任何EmbeddedServletContainerCustomizers,则还需要导入EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar.class.
回答by frno
As of Spring Boot 2.0 only spring.main.web-application-type=nonein the relevant profile do the trick.
从 Spring Boot 2.0 开始,只有spring.main.web-application-type=none在相关配置文件中才能做到这一点。
If you use a multi-document application.ymlwith Spring Boot 2.0, adding this block and replacing no-web-profile-namewith the profile that shouldn't have an embedded web server should work:
如果您application.yml在 Spring Boot 2.0 中使用多文档,添加此块并替换no-web-profile-name为不应具有嵌入式 Web 服务器的配置文件应该可以工作:
---
spring:
profiles: no-web-profile-name
main:
web-application-type: none
回答by barcom
The answers from @hzpz and @orid set me on the right track.
@hzpz 和 @orid 的回答让我走上了正轨。
I needed to add
我需要添加
@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,
WebMvcAutoConfiguration.class})
and set:
并设置:
spring.main.web-environment=false
in my application.propertiesfile for the non-Rest cases.
在我application.properties的非 Rest 案例文件中。

