Nico Gallegos
Nico Gallegos

Reputation: 421

Multiple SpringBoot apps, want to run only one

I'm currently working with SpringBootApplications, I have two differets @SpringBootApplication , one for Web Application, and a CommandLineRunner .

The problem is, no matter which of them I execute, it tries to run both of the applications.

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

And this is my other 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);
}

   }

If I run the CommandLineRunner app, after it is executed, continues to load the Web App.
I need to be able to run each of them separately from each one. But I don't know how to configure this.

Thanks!

Upvotes: 2

Views: 2640

Answers (1)

Sander Hautvast
Sander Hautvast

Reputation: 31

the spring docs say:

  1. SpringBootApplication: This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.

  2. You should only ever add one @EnableAutoConfiguration annotation. We generally recommend that you add it to your primary @Configuration class.

so effectively you're adding 2 EnableAutoConfiguration annotations which just isn't allowed by spring boot. I would suggest using spring Profiles to achieve what you need.

Upvotes: 2

Related Questions