关于spring boot如何正确禁用web环境 [英] about spring boot how to disable web environment correctly

查看:151
本文介绍了关于spring boot如何正确禁用web环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 启动非 Web 应用程序,启动时出现以下错误

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]

然后我尝试了以下方式

new SpringApplication().setWebEnvironment(false);

然后启动它仍然出现上述错误.

then start it still have above error.

然后尝试

@SpringBootApplication(exclude={SpringDataWebAutoConfiguration.class})

但仍然有同样的错误.

最后我尝试在 application.properties

spring.main.web-environment=false

这次成功了.

为什么前两种方式不起作用?

Why the first two manner cannot work?

推荐答案

此答案已过时.请注意 Spring Boot 2.0 的其他答案

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

您在 new SpringApplication() 对象中禁用 setWebEnvironment(false) 并在 SpringApplication 上调用静态方法 run().run(...) 这是不同的.

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

我想出了 3 种方法来做到这一点:

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 ;)");
    }
}

这是完整的工作项目.

并且您不需要排除以下配置:

And you don't need to exclude below config:

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

因为您的 pom.xml

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

这篇关于关于spring boot如何正确禁用web环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆