I'm using spring-boot
and added spring-web
dependency in maven pom, to make use of RestTemplate
.
我正在使用spring-boot并在maven pom中添加spring-web依赖,以使用RestTemplate。
Now spring tries to initialize an EmbeddedServletContext
. How can I prevent it?
现在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
4 个解决方案
#1
27
For reference: This use case is documented in the Spring Boot Reference Guide:
供参考:Spring Boot Reference Guide中记录了此用例:
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 theSpringApplication
features of Spring Boot. ASpringApplication
changes itsApplicationContext
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 callSpringApplication.setWebEnvironment(false)
, or set theapplicationContextClass
property (through the Java API or with external properties). Application code that you want to run as your business logic can be implemented as aCommandLineRunner
and dropped into the context as a@Bean
definition.并非所有Spring应用程序都必须是Web应用程序(或Web服务)。如果要在main方法中执行某些代码,还要引导Spring应用程序来设置要使用的基础结构,那么使用Spring Boot的SpringApplication功能很容易。 SpringApplication根据它是否认为需要Web应用程序来更改其ApplicationContext类。您可以做的第一件事就是将servlet API依赖项留在类路径中。如果您不能这样做(例如,您正在运行来自相同代码库的2个应用程序),那么您可以显式调用SpringApplication.setWebEnvironment(false),或者设置applicationContextClass属性(通过Java API或使用外部属性)。您希望作为业务逻辑运行的应用程序代码可以作为CommandLineRunner实现,并作为@Bean定义放入上下文中。
application.properties:
application.properties:
spring.main.web-environment=false #webEnvironment property
#2
21
First trick:
第一招:
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class)
.web(false)
.run(args);
}
Second:
第二:
@Configuration
@EnableAutoConfiguration(exclude = WebMvcAutoConfiguration.class)
public class Application {
#3
2
Although the legacy way of disabling the web environment (as mentioned in @Tim's answer) still works in Spring Boot 2.x, however the new preferred way is by setting the following property
虽然禁用Web环境的传统方法(如@Tim的回答中提到的)仍然适用于Spring Boot 2.x,但是新的首选方法是设置以下属性
spring.main.web-application-type=none
spring.main.web应用型=无
Here is the enum that defines the allowed values
以下是定义允许值的枚举
#4
2
This works in Spring Boot 2.x
这适用于Spring Boot 2.x.
public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class)
.web(WebApplicationType.NONE)
.build()
.run(args);
}
#1
27
For reference: This use case is documented in the Spring Boot Reference Guide:
供参考:Spring Boot Reference Guide中记录了此用例:
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 theSpringApplication
features of Spring Boot. ASpringApplication
changes itsApplicationContext
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 callSpringApplication.setWebEnvironment(false)
, or set theapplicationContextClass
property (through the Java API or with external properties). Application code that you want to run as your business logic can be implemented as aCommandLineRunner
and dropped into the context as a@Bean
definition.并非所有Spring应用程序都必须是Web应用程序(或Web服务)。如果要在main方法中执行某些代码,还要引导Spring应用程序来设置要使用的基础结构,那么使用Spring Boot的SpringApplication功能很容易。 SpringApplication根据它是否认为需要Web应用程序来更改其ApplicationContext类。您可以做的第一件事就是将servlet API依赖项留在类路径中。如果您不能这样做(例如,您正在运行来自相同代码库的2个应用程序),那么您可以显式调用SpringApplication.setWebEnvironment(false),或者设置applicationContextClass属性(通过Java API或使用外部属性)。您希望作为业务逻辑运行的应用程序代码可以作为CommandLineRunner实现,并作为@Bean定义放入上下文中。
application.properties:
application.properties:
spring.main.web-environment=false #webEnvironment property
#2
21
First trick:
第一招:
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class)
.web(false)
.run(args);
}
Second:
第二:
@Configuration
@EnableAutoConfiguration(exclude = WebMvcAutoConfiguration.class)
public class Application {
#3
2
Although the legacy way of disabling the web environment (as mentioned in @Tim's answer) still works in Spring Boot 2.x, however the new preferred way is by setting the following property
虽然禁用Web环境的传统方法(如@Tim的回答中提到的)仍然适用于Spring Boot 2.x,但是新的首选方法是设置以下属性
spring.main.web-application-type=none
spring.main.web应用型=无
Here is the enum that defines the allowed values
以下是定义允许值的枚举
#4
2
This works in Spring Boot 2.x
这适用于Spring Boot 2.x.
public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class)
.web(WebApplicationType.NONE)
.build()
.run(args);
}