Multiple @SpringBootApplication Annotation in a Project
I recently faced the same scenario here and I solved with a simple solution.
My projected uses Maven and is configured with sub-modules like this:
my-parent
|__ my-main (depends on my-other module)
|__ my-other
Each module has its own main App class annotated with @SpringBootApplication. The problem is that both classes reside in the same package even though they are in different modules.
Once I start MyMainApp it also starts MyOtherApp. To avoid this I just had to do the following.
In the my-main module I have:
@SpringBootApplication
public class MyMainApp ... { ... }
and in the my-other module I have:
@SpringBootApplication
@ConditionalOnProperty(name = "my.other.active", havingValue = "true", matchIfMissing = false)
public class MyOtherApp ... { ... }
with application.properties with:
my.other.active=true
It works as expected.
The @SpringBootApplication
annotation is a shortcut annotation for @Configuration
, @EnableAutoConfiguration
, and @ComponentScan
.
http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html
The default behavior of @ComponentScan
is to look for @Configuration
and @Component
classes within the same package and all sub-packages of the annotated class. Since all your classes are in the same package, when you start any one of them Spring will find the others and treat them like @Configuration
classes, and register their beans, etc.
So yes, this is expected behavior given your project setup. Put each @SpringBootApplication
class in a separate subpackage if you don't want this to happen for local testing. If this moves beyond a demo at some point you'll probably want to come up with a better setup (subprojects for each @SpringBootApplication
perhaps).