如何防止spring-web的弹簧自动配置? [英] How to prevent spring-boot autoconfiguration for spring-web?

查看:115
本文介绍了如何防止spring-web的弹簧自动配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using spring-boot and added spring-web dependency in maven pom, to make use of RestTemplate.

现在spring尝试初始化 EmbeddedServletContext 。如何防止它?

Now spring tries to initialize an EmbeddedServletContext. How can I prevent it?

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


推荐答案

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

For reference: This use case is documented in the Spring Boot Reference Guide:


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

Not all Spring applications have to be web applications (or web services). If you want to execute some code in a main method, but also bootstrap a Spring application to set up the infrastructure to use, then it’s easy with the SpringApplication features of Spring Boot. A SpringApplication changes its ApplicationContext class depending on whether it thinks it needs a web application or not. The first thing you can do to help it is to just leave the servlet API dependencies off the classpath. If you can’t do that (e.g. you are running 2 applications from the same code base) then you can explicitly call SpringApplication.setWebEnvironment(false), or set the applicationContextClass property (through the Java API or with external properties). Application code that you want to run as your business logic can be implemented as a CommandLineRunner and dropped into the context as a @Bean definition.

application.properties:

application.properties:

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

这篇关于如何防止spring-web的弹簧自动配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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