首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何防止spring-web的spring-boot自动配置?

如何防止spring-web的spring-boot自动配置?
EN

Stack Overflow用户
提问于 2015-04-22 22:22:50
回答 3查看 49.4K关注 0票数 34

我正在使用spring-boot,并在maven pom中添加了spring-web依赖项,以利用RestTemplate

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

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

发布于 2015-04-22 22:32:48

第一个技巧:

代码语言:javascript
复制
public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class)
                .web(false)
                .run(args);
}

第二:

代码语言:javascript
复制
@Configuration
@EnableAutoConfiguration(exclude = WebMvcAutoConfiguration.class)
public class Application {
票数 28
EN

Stack Overflow用户

发布于 2017-10-04 23:44:19

尽管传统的禁用web环境的方法(如@Tim的答案中提到的)在Spring Boot2.x中仍然有效,但是新的首选方法是通过设置以下属性

spring.main.web-application-type=none

Here是定义允许值的枚举

票数 16
EN

Stack Overflow用户

发布于 2018-04-25 18:39:20

这在Spring Boot 2.x中有效

代码语言:javascript
复制
public static void main(String[] args) {
   new SpringApplicationBuilder(MyApplication.class)
            .web(WebApplicationType.NONE)
            .build()
            .run(args);
}
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29800584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档