Spring boot 关于spring boot如何正确禁用web环境
Spring启动非web应用程序,启动时出现以下错误Spring boot 关于spring boot如何正确禁用web环境,spring-boot,Spring Boot,Spring启动非web应用程序,启动时出现以下错误 Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. at org.springframework.boot.context.embedded.EmbeddedWeb
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:185) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
然后我试着用下面的方式
new SpringApplication().setWebEnvironment(false);
然后启动它仍然有上述错误
然后尝试
@SpringBootApplication(exclude={SpringDataWebAutoConfiguration.class})
但还是有同样的错误
最后,我尝试在application.properties中添加以下配置
spring.main.web-environment=false
这次成功了
为什么前两种方式不起作用?这个答案已经过时了。请注意Spring Boot 2.0的另一个答案
Spring Boot 1.x的原始答案:
此配置不工作的原因是因为以下两个不同的实例:
new SpringApplication().setWebEnvironment(false);
SpringApplication.run(SpringBootDisableWebEnvironmentApplication.class, args);
您正在禁用newspringapplication()
对象中的setWebEnvironment(false)
,并在SpringApplication.run(…)
上调用静态方法run()
我想出了三种方法:
@SpringBootApplication
public class SpringBootDisableWebEnvironmentApplication implements CommandLineRunner{
public static void main(String[] args) throws Exception {
// Method#1: Using SpringApplicationBuilder.
SpringApplication springApplication =
new SpringApplicationBuilder()
.sources(SpringBootDisableWebEnvironmentApplication.class)
.web(false)
.build();
springApplication.run(args);
//--------------------------------------------------------
// Method#2: Using SpringBootDisableWebEnvironmentApplication.
// SpringBootDisableWebEnvironmentApplication springBootDisableWebEnvironmentApplication =
// new SpringBootDisableWebEnvironmentApplication();
// springBootDisableWebEnvironmentApplication.run(args);
//--------------------------------------------------------
// Method#3: Using SpringApplication().
// SpringApplication springApplication = new SpringApplication();
// springApplication.setWebEnvironment(false);
//
// Set<Object> sources = new HashSet<>();
// sources.add(SpringBootDisableWebEnvironmentApplication.class);
// springApplication.setSources(sources);
// springApplication.run(args);
//--------------------------------------------------------
}
@Override
public void run(String... arg0) throws Exception {
System.out.println("Hello, Spring Boot gives many options ;)");
}
}
因为在pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
org.springframework.boot
SpringBootStarterWeb
从Spring Boot 2.0开始
-web(false)/setWebEnvironment(false)已弃用,可以使用web应用程序类型来指定
-
- 或
其中:
NONE
-应用程序不应作为web应用程序运行,也不应启动嵌入式web服务器
REACTIVE
-应用程序应作为REACTIVE web应用程序运行,并应启动嵌入式REACTIVE web服务器
SERVLET
-应用程序应作为基于SERVLET的web应用程序运行,并应启动嵌入式SERVLET web服务器
礼貌:发布实际代码,而不是代码片段。。。main
方法中有更多行。因此,发布应用程序类。尝试此@springbootplication(exclude={EmbeddedServletContainerAutoConfiguration.class,WebMvcAutoConfiguration.class})
,因为@SanjayRawat仍然不起作用,请看@M.Deinum请看谢谢@Sanjay Rawat但如果只能通过setwebenvironment(false)
禁用web环境,就不能通过排除一些关键类来实现this@zhuguowei参见方法#3。这个答案已经过时了。请注意Spring Boot 2.0的另一个答案感谢您指向Spring Boot 2解决方案;我正在寻找从Spring1开始的迁移。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring.main.web-application-type=NONE
# REACTIVE, SERVLET
@SpringBootApplication
public class SpringBootDisableWebEnvironmentApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(SpringBootDisableWebEnvironmentApplication.class)
.web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
.run(args);
}
}