Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

One of those Configuration classes uses a REST interface, and therefore I'm including spring-boot-starter-web as a dependency.

This starts up an embedded Tomcat instance, which is fine.

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

Is there any way to stop the @SpringBootApplication from starting up Tomcat by default, and only using it for the REST Configuration class? E.g., by annotating that class with @EnableWebMVC

Here's an example of my @Configurationclasses:

REST:

@Profile({"REST"}) @Configuration @EnableWebMvc public class HttpConfiguration{  .  .  . } 

JMS:

@Profile({"JMS"}) @Configuration @EnableJms public class JMSConfiguration{  .  .  . } 

Thanks

like image 968
barcom Avatar asked Aug 18 '15 16:08

barcom


People also ask

How do I stop embedded Tomcat in spring boot?

If we want to exclude tomcat from spring boot, we don't need to do much, we just need to add one additional block(<exclusions>) to the Spring Boot dependency. <exclusions> tag is used to make us sure that given server/artifactId is being removed at the time of build.

Can we override or replace the embedded Tomcat server in spring boot?

Can we override or replace the Embedded tomcat server in Spring Boot? Yes, we can replace the Embedded Tomcat server with any server by using the Starter dependency in the pom. xml file .

Can we override embedded server in spring boot?

The default Embedded Web Servers in Spring-Boot is Tomcat , but you can easily change it to others.


2 Answers

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.

like image 97
hzpz Avatar answered Oct 21 '22 13:10

hzpz


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 
like image 25
frno Avatar answered Oct 21 '22 14:10

frno