2016-05-12 11 views
1

Frühling Boot Nicht-Web-Anwendung, zu deaktivieren, wenn die Diskussion unter Fehlern unten Art und Weiseüber Federverschluß, wie Web-Umgebung richtig

new SpringApplication().setWebEnvironment(false); 
es

dann noch habe ich

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] 

Dann versucht hat, oben beginnen Error.

versucht Dann

@SpringBootApplication(exclude={SpringDataWebAutoConfiguration.class}) 

aber immer noch die gleichen Fehler.

Endlich habe ich versucht, in application.properties

spring.main.web-environment=false 

diesmal funktioniert es unter Konfiguration hinzuzufügen.

Warum die ersten beiden Arten können nicht funktionieren?

+1

Beitrag der eigentliche Code nicht einen Schnipsel ... Es sind mehr Zeilen in Ihrer 'Haupt' Methode. Also poste die Anwendungsklasse. –

+0

Versuchen Sie, diese '@SpringBootApplication (exclude = {EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class})' als [Empfohlene hier] (http://stackoverflow.com/questions/32078015/spring-boot-enable-disable-embedded -tomcat-with-profile) –

+0

@SanjayRawat immer noch nicht funktionieren, siehe https://github.com/zhugw/spring-boot-disable-web-environment – zhuguowei

Antwort

2

Der Grund dieser Konfiguration nicht funktioniert, weil diese zwei verschiedene Instanzen sind:

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

Sie deaktivieren setWebEnvironment(false) in new SpringApplication() Objekt und statische Methode run() auf SpringApplication.run(...) Aufruf, die anderes ist.

dachte ich 3 Auswege, dies zu tun:

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

die komplette Arbeits Project ist.

Und Sie brauchen nicht unter Config auszuschließen:

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

Da haben Sie nicht spring-boot-starter-web Abhängigkeit in Ihrem pom.xml

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

Dank @Sanjay Rawat, aber wenn Web-Umgebung durch 'setWebEnviroment (false)' nur deaktiviert werden könnte, kann durch das Ausschließen einiger Schlüsselklassen nicht erreicht werden – zhuguowei

+0

@zhuguowei Siehe Methode # 3. –

Verwandte Themen