2016-11-04 45 views
1

現在SpringBootApplicationsで作業しています.2つの異なる@SpringBootApplication、1つはWebアプリケーション、もう1つはCommandLineRunnerです。複数のSpringBootアプリ、1つだけを実行したい

問題は、どちらのアプリケーションを実行しても、両方のアプリケーションを実行しようとすることです。

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

[実行可能なjarにどのメインクラスを使用するかSpring Bootに指示するにはどうすればよいですか?](http://stackoverflow.com/questions/23217002/how-do-i-tell-spring-boot-which-main -class-to-the-executable-jar) –

+0

コードをビルドするためにmavenを使用していますか? – Jobin

答えて

1

春のドキュメントは言う:

  1. SpringBootApplication:これは@Configuration、@EnableAutoConfigurationと@ComponentScanを宣言することと等価であるコンビニエンス注釈です。

  2. @EnableAutoConfigurationアノテーションは1つだけ追加してください。一般に、@Configurationクラスのプライマリクラスに追加することをお勧めします。

効果的に、2つのEnableAutoConfiguration注釈を追加していますが、これは春の起動では許可されません。私はあなたが必要なものを達成するために春のプロファイルを使用することをお勧めします。

関連する問題