[英]Spring boot enable/disable embedded tomcat with profile
I'm writing a Spring Boot application that uses one of several @Configuration
classes depending on which @Profile
is set in the application.properties
file. 我正在编写一个Spring Boot应用程序,该应用程序使用几个
@Configuration
类中的一个,具体取决于在application.properties
文件中设置的@Profile
。
One of those Configuration classes uses a REST interface, and therefore I'm including spring-boot-starter-web
as 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 (eg I'm using JMS to handle incoming messages instead of REST). 问题是,其他配置文件不需要嵌入式服务器(例如,我使用JMS来处理传入的消息而不是REST)。
Is there any way to stop the @SpringBootApplication
from starting up Tomcat by default, and only using it for the REST Configuration class? 是否有任何方法可以阻止
@SpringBootApplication
默认启动Tomcat,并且仅将它用于REST配置类? Eg, by annotating that class with @EnableWebMVC
例如,通过使用
@EnableWebMVC
注释该类
Here's an example of my @Configuration
classes: 这是我的
@Configuration
类的一个例子:
REST: 休息:
@Profile({"REST"})
@Configuration
@EnableWebMvc
public class HttpConfiguration{
.
.
.
}
JMS: JMS:
@Profile({"JMS"})
@Configuration
@EnableJms
public class JMSConfiguration{
.
.
.
}
Thanks 谢谢
Use 采用
@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,
WebMvcAutoConfiguration.class})
to exclude Spring Boot's auto-configuration for embedded servlet containers. 排除Spring Boot的嵌入式servlet容器的自动配置。 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): 此外,您需要为非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: 然后通过导入
EmbeddedServletContainerAutoConfiguration.class
在您的REST配置文件中启用嵌入式Tomcat(这会延迟自动配置,直到加载REST配置文件之后:
@Profile({"REST"})
@Configuration
@Import(EmbeddedServletContainerAutoConfiguration.class)
public class HttpConfiguration {
// ...
}
If you are using any EmbeddedServletContainerCustomizer
s, you also need to import EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar.class
. 如果您使用的是任何
EmbeddedServletContainerCustomizer
,则还需要导入EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar.class
。
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.properties
file for the non-Rest cases. 在我的
application.properties
文件中,用于非Rest案例。
As of Spring Boot 2.0 only spring.main.web-application-type=none
in the relevant profile do the trick. 从Spring Boot 2.0
spring.main.web-application-type=none
,相关配置文件中只有spring.main.web-application-type=none
可以解决问题。
If you use a multi-document application.yml
with Spring Boot 2.0, adding this block and replacing no-web-profile-name
with the profile that shouldn't have an embedded web server should work: 如果在Spring Boot 2.0中使用多文档
application.yml
,则添加此块并将no-web-profile-name
替换为不应具有嵌入式Web服务器的配置文件应该可以正常工作:
---
spring:
profiles: no-web-profile-name
main:
web-application-type: none
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.