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?
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) .
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.
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.
-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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With