Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about spring boot how to disable web environment correctly

Tags:

Spring boot non-web application, when start it has below error

Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 

Then I tried below manner

new SpringApplication().setWebEnvironment(false); 

then start it still have above error.

Then tried

@SpringBootApplication(exclude={SpringDataWebAutoConfiguration.class}) 

but still have the same error.

At last I tried add below configuration in application.properties

spring.main.web-environment=false 

this time it works.

Why the first two manner cannot work?

like image 611
zhuguowei Avatar asked May 12 '16 12:05

zhuguowei


People also ask

Can spring boot disable default Web server?

Another way to disable the embedded web server in Spring Boot is by using code. We can use either the SpringApplicationBuilder: new SpringApplicationBuilder(MainApplication. class) .

Can we create a non web application in spring boot?

The application can also be called as Spring Boot Standalone application. To develop a non web application, your Application needs to implement CommandLineRunner interface along with its run method. This run method acts like a main of your application.

What is the importance of @SpringBootApplication?

Spring Boot @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. It's same as declaring a class with @Configuration, @EnableAutoConfiguration and @ComponentScan annotations.


1 Answers

Starting from Spring Boot 2.0

-web(false)/setWebEnvironment(false) is deprecated and instead Web-Application-Type can be used to specify

  • Application Properties

    spring.main.web-application-type=NONE  # REACTIVE, SERVLET 
  • or SpringApplicationBuilder

    @SpringBootApplication public class SpringBootDisableWebEnvironmentApplication {      public static void main(String[] args) {         new SpringApplicationBuilder(SpringBootDisableWebEnvironmentApplication.class)             .web(WebApplicationType.NONE) // .REACTIVE, .SERVLET             .run(args);    } } 

Where WebApplicationType:

  • NONE - The application should not run as a web application and should not start an embedded web server.
  • REACTIVE - The application should run as a reactive web application and should start an embedded reactive web server.
  • SERVLET - The application should run as a servlet-based web application and should start an embedded servlet web server.

Courtesy: Another SO Answer

like image 111
Narasimha Avatar answered Sep 20 '22 22:09

Narasimha