2016-11-04 37 views
1

我目前正在使用SpringBootApplications,我有兩個不同的@SpringBootApplication,一個用於Web應用程序和一個CommandLineRunner。多個SpringBoot應用程序,只想運行一個

問題是,無論我執行哪個,它都會嘗試運行這兩個應用程序。

package com.ws; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.context.web.SpringBootServletInitializer; 
import org.springframework.context.annotation.Configuration; 

@EnableAutoConfiguration 
public class Init extends SpringBootServletInitializer { 

@Override 
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
    return application.sources(Init.class); 
} 

/** 
* Main method. 
* 
* @param args String[]. 
* @throws Exception Exception. 
*/ 
public static void main(String[] args) throws Exception { 
    SpringApplication.run(Init.class, args); 
} 

這是我的其他InitBatch.java:

package com.batch; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.CommandLineRunner; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 


@SpringBootApplication 
public class InitBatch implements CommandLineRunner { 

@Autowired 
private Batch batch; 

@Override 
public void run(String... args) throws Exception { 
    batch.processFiles();  
} 

public static void main(String[] args) throws Exception { 
    SpringApplication.run(InitBatch.class, args); 
} 

    } 

如果我運行CommandLineRunner應用程序,執行完成後,繼續加載Web應用程序。
我需要能夠分別從每個運行它們中的每一個。但我不知道如何配置它。

謝謝!

+0

請將軟件包加入主類源。 – luboskrnac

+0

可能的重複[如何告訴Spring Boot哪個主要類用於可執行jar?](http://stackoverflow.com/questions/23217002/how-do-i-tell-spring-boot-which-main -class-to-the-the-the-executable-jar) –

+0

你使用maven來構建你的代碼嗎? – Jobin

回答

1

彈簧文檔說:

  1. SpringBootApplication:這是一個方便的標註,它等效於聲明@Configuration,@EnableAutoConfiguration和@ComponentScan。

  2. 您應該只添加一個@EnableAutoConfiguration註釋。我們通常建議您將它添加到您的主要@Configuration類中。

如此有效地添加了2個EnableAutoConfiguration註釋,這些註釋只是Spring引導不允許的。我會建議使用彈簧配置文件來實現你所需要的。

相關問題