about spring boot how to disable web environment correctly

ghz 1years ago ⋅ 5547 views

Question

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?


Answer

This answer is obsolete. Please note the other answer for Spring Boot 2.0

Original answer for Spring Boot 1.x:

The reason this config is not working because these are two different instances:

new SpringApplication().setWebEnvironment(false); 
SpringApplication.run(SpringBootDisableWebEnvironmentApplication.class, args);

You are disabling setWebEnvironment(false) in new SpringApplication() object and calling static method run() on SpringApplication.run(...) which is different one.

I figured out 3 ways to do this:

@SpringBootApplication
public class SpringBootDisableWebEnvironmentApplication implements CommandLineRunner{


    public static void main(String[] args) throws Exception {

//      Method#1: Using SpringApplicationBuilder.

        SpringApplication springApplication = 
                new SpringApplicationBuilder()
                .sources(SpringBootDisableWebEnvironmentApplication.class)
                .web(false)
                .build();

        springApplication.run(args);

//--------------------------------------------------------      

//      Method#2: Using SpringBootDisableWebEnvironmentApplication.     

//      SpringBootDisableWebEnvironmentApplication springBootDisableWebEnvironmentApplication = 
//              new SpringBootDisableWebEnvironmentApplication();
//      springBootDisableWebEnvironmentApplication.run(args);

//--------------------------------------------------------      

//      Method#3: Using SpringApplication().

//      SpringApplication springApplication = new SpringApplication();
//      springApplication.setWebEnvironment(false);
//      
//      Set<Object> sources = new HashSet<>();
//      sources.add(SpringBootDisableWebEnvironmentApplication.class);
//      springApplication.setSources(sources);
//      springApplication.run(args);

//--------------------------------------------------------  

    }

    @Override
    public void run(String... arg0) throws Exception {
        System.out.println("Hello, Spring Boot gives many options ;)");
    }
}

Here is the complete working [Project](https://github.com/RawSanj/SOFRepos/tree/master/spring-boot-disable- web-environment).

And you don't need to exclude below config:

@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class, 
                              WebMvcAutoConfiguration.class})

Because you don't have spring-boot-starter-web dependency in your pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>