如何防止弹簧网的弹簧自动配置?

mem*_*und 30 java spring spring-boot spring-web

我正在使用spring-boot并添加spring-web依赖于maven pom,以便利用RestTemplate.

现在春天试图初始化一个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
Run Code Online (Sandbox Code Playgroud)

Tim*_*Tim 33

供参考:Spring Boot Reference Guide中记录了此用例:

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

application.properties:

spring.main.web-environment=false   #webEnvironment property
Run Code Online (Sandbox Code Playgroud)

  • 未弃用的版本现在是 spring.main.web-environment-type=none (5认同)

Art*_*lan 26

第一招:

public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class)
                .web(false)
                .run(args);
}
Run Code Online (Sandbox Code Playgroud)

第二:

@Configuration
@EnableAutoConfiguration(exclude = WebMvcAutoConfiguration.class)
public class Application {
Run Code Online (Sandbox Code Playgroud)

  • `spring.main.web_environment = false`虽然更好......不需要编写任何Java代码:-) (7认同)

geo*_*and 10

虽然禁用Web环境的传统方式(如@Tim的回答中提到的)仍然适用于Spring Boot 2.x,但是新的首选方法是设置以下属性

spring.main.web-application-type=none

是定义允许值的枚举


小智 9

这适用于Spring Boot 2.x.

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