2016-05-12 51 views
1

春季启动非Web应用程序,当启动它具有以下错误关于春天开机如何禁用网络环境中正常

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 

这一次它的工作原理。

为什么前两种方式无法正常工作?

+1

spring-boot-starter-web依赖实际的代码不是片段......有在'main'方法中有更多的行。所以发布应用程序类。 –

+0

尝试此'@SpringBootApplication(排除= {EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class})' 作为 [参考](http://stackoverflow.com/questions/32078015/spring-boot-enable-disable-embedded -Tomcat-与知名度) –

+0

@SanjayRawat还是不行,请参阅https://github.com/zhugw/spring-boot-disable-web-environment – zhuguowei

回答

2

此配置不工作,因为这是两个不同的实例原因:

new SpringApplication().setWebEnvironment(false); 
SpringApplication.run(SpringBootDisableWebEnvironmentApplication.class, args); 

您要停用在new SpringApplication()对象setWebEnvironment(false)SpringApplication.run(...)这是一个不同的调用静态方法run()

我想通了3种方式来做到这一点:

@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 ;)"); 
    } 
} 

下面是完整的工作Project

而你并不需要排除以下配置:

@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class, 
           WebMvcAutoConfiguration.class}) 

因为你没有在你的pom.xml

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-web</artifactId> 
</dependency>  
+0

感谢@Sanjay拉瓦特但如果只可以通过'setWebEnviroment禁用网络环境(假)',不能排除通过一些关键的类来实现这个 – zhuguowei

+0

@zhuguowei请参见方法3。 –