多个SpringBoot应用程序,只想运行一个

问题描述:

我目前正在使用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://*.com/questions/23217002/how-do-i-tell-spring-boot-which-main -class-to-the-the-the-executable-jar) –

+0

你使用maven来构建你的代码吗? – Jobin

弹簧文档说:

  1. SpringBootApplication:这是一个方便的标注,它等效于声明@Configuration,@EnableAutoConfiguration和@ComponentScan。

  2. 您应该只添加一个@EnableAutoConfiguration注释。我们通常建议您将它添加到您的主要@Configuration类中。

如此有效地添加了2个EnableAutoConfiguration注释,这些注释只是Spring引导不允许的。我会建议使用弹簧配置文件来实现你所需要的。