Java 如何防止SpringWeb的SpringBoot自动配置?

Java 如何防止SpringWeb的SpringBoot自动配置?,java,spring,spring-boot,spring-web,Java,Spring,Spring Boot,Spring Web,我正在使用springboot并在maven pom中添加springweb依赖项,以利用restemplate 现在spring尝试初始化一个EmbeddedServletContext。我怎样才能预防它 Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.sp

我正在使用
springboot
并在maven pom中添加
springweb
依赖项,以利用
restemplate

现在spring尝试初始化一个
EmbeddedServletContext
。我怎样才能预防它

Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
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:183)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
    ... 8 more
第一个窍门:

public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class)
                .web(false)
                .run(args);
}
第二:

@Configuration
@EnableAutoConfiguration(exclude = WebMvcAutoConfiguration.class)
public class Application {

供参考:本用例记录在:

并非所有Spring应用程序都必须是web应用程序(或web服务)。如果您想在
main
方法中执行一些代码,但也要引导一个Spring应用程序来设置要使用的基础结构,那么使用Spring Boot的
SpringApplication
功能就很容易了。
SpringApplication
根据是否需要web应用程序来更改其
ApplicationContext
类。要帮助它,您可以做的第一件事就是将servlet API依赖项从类路径中移除。如果您不能这样做(例如,您正在从同一代码库运行两个应用程序),那么您可以显式调用
SpringApplication.setWebEnvironment(false)
,或者设置
applicationContextClass
属性(通过Java API或使用外部属性)。要作为业务逻辑运行的应用程序代码可以实现为
CommandLineRunner
,并作为
@Bean
定义放到上下文中

application.properties:

spring.main.web-environment=false   #webEnvironment property

尽管在SpringBoot2.x中禁用web环境的传统方法(如@Tim的回答中所述)仍然有效,但是新的首选方法是设置以下属性

spring.main.web应用程序类型=none


是定义允许值的枚举

这适用于Spring Boot 2.x

public static void main(String[] args) {
   new SpringApplicationBuilder(MyApplication.class)
            .web(WebApplicationType.NONE)
            .build()
            .run(args);
}

我试图用Spring Boot 2.06构建一个反应式Web应用程序,但出现以下错误:

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at com.vogella.springboot2.Test3Application.main(Test3Application.java:10) [main/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    ... 8 common frames omitted
此链接解释了问题,并提供了两个建议:

我正在用SpringBoot构建一个新的SpringWebFlux应用程序。 从start.spring.io下载项目模板后,我添加了 一些第三方依赖项,并试图启动应用程序。然后 我遇到了错误 org.springframework.context.ApplicationContextException:无法 由于缺少,请启动ServletWebServerApplicationContext ServletWebServerFactorybean

错误消息是解决方案的线索。上面说无法启动 ServletWebServerApplicationContext,但我的项目正在使用WebFlux 它是一个反应式web项目,而不是基于servlet的SpringWebMVC 项目

调试Spring源代码帮助我找到了原因。在 方法导出的WebApplicationType org.springframework.boot.SpringApplication,web应用程序类型 仅当Spring Web的类 MVC不在类路径中

一旦确定了根本原因,解决方案就很容易了。我们可以:

更新Maven依赖项以排除spring webmvc,或设置web 将应用程序类型显式转换为WebApplicationType.REACTIVE,如图所示 下面


瞧!问题解决了

我尝试了
@EnableAutoConfiguration(exclude=WebMvcAutoConfiguration.class,EmbeddedServletContainerAutoConfiguration.class})
,但仍然出现相同的错误。。。如果
SpringWeb
的自动配置可以通过注释禁用,而无需修改我的主启动程序方法,那就太好了……尽管您的建议很有效,谢谢!但无论如何,最好只禁用自动配置注释。不过,
spring.main.web\u environment=false
更好。。。不需要编写任何Java:-)@membersound代码,我今天也遇到了同样的问题。我最终放弃了REST模板进行改造:。。。首先,它从我的类路径中消除了SpringWeb依赖性。第二,它更容易推理和阅读。链接页面没有给出任何关于
spring.main.web应用程序类型
属性的信息(当我写这篇文章时),甚至没有给出有效值。搜索
SpringWebApplicationType API
会发现有效值是
servlet
reactive
none
(我猜是小写的)。@ddekany他们一定更改了文档。谢谢你指出这一点!现在,未弃用的版本是spring.main.web环境类型=none
@SpringBootApplication class MyApplication
fun main(args: Array<String>) {
    val app = SpringApplication(MyApplication::class.java)
    app.webApplicationType = WebApplicationType.REACTIVE
    app.run(*args)
}
spring.main.web-application-type=reactive