Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Manage Multiple Spring Boot in the same project

Tags:

spring-boot

Learning Spring Boot as a replacement for some C/C++ daemons. My goal was to have a single project compiled to a single jar/war. Then use multiple shell scripts to simply start the one I want via command line. I'm using Eclipse Kepler to develop and test the individual Spring Boot applications, and noticed an unwanted behavior. With 4 Spring Boot application classes in the same package, if I start any one of them using an Eclipse launch configuration, all 4 start up in the same Spring Boot. I suspect because all of them have the @SpringBootApplication annotation, and starting one causes Spring Boot to "scan" the current package and sub-packages.

My question, is there a way to have multiple Spring Boot applications in the same package? Do I just create a ControllerApplication with a single @SpringBootApplication and pass in the daemon name I want to start and go from there? Or some other options? Or do I need to create a separate project for each daemon? tia, adym

like image 876
lincolnadym Avatar asked Dec 14 '22 03:12

lincolnadym


1 Answers

You could annotate your Spring Boot Application with @ComponentScan's excludeFilters

@ComponentScan(basePackages = "your.package", 
               excludeFilters = @Filter(SpringBootApplication.class)) 

You may have many dependency issues from this point on, you can resolve them using the same principle.

However

This may work inside your IDE, but the generated spring boot jar will only have a single main class attribute. Because of this, even if you find workarounds, I believe you should either :

  • package each spring boot application in its own maven project;
  • OR have a single Spring Boot application with multiple Spring profiles.

Take a look at these other answers to launch your spring boot application with multiple spring profiles:

  • spring boot - launch a server and a client
  • spring boot - launch twice with different ports
like image 77
alexbt Avatar answered Feb 14 '23 00:02

alexbt