Spring boot enable/disable embedded tomcat with profile

前端 未结 3 1624
囚心锁ツ
囚心锁ツ 2020-12-05 13:49

I\'m writing a Spring Boot application that uses one of several @Configuration classes depending on which @Profile is set in the application.

相关标签:
3条回答
  • 2020-12-05 14:19

    As of Spring Boot 2.0 only spring.main.web-application-type=none in the relevant profile do the trick.

    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:
      profiles: no-web-profile-name
      main:
        web-application-type: none
    
    0 讨论(0)
  • 2020-12-05 14:21

    The answers from @hzpz and @orid set me on the right track.

    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.

    0 讨论(0)
  • 2020-12-05 14:24

    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.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:

    @Profile({"REST"})
    @Configuration
    @Import(EmbeddedServletContainerAutoConfiguration.class)
    public class HttpConfiguration {
        // ...
    }
    

    If you are using any EmbeddedServletContainerCustomizers, you also need to import EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar.class.

    0 讨论(0)
提交回复
热议问题