Spring Boot by default will scan all the classes in the package the @SpringBootApplication class is defined in and all of its sub-packages. It will setup Spring Data JPA, scan for entities and what more in those packages.
Consider the following configuration
@SpringBootApplication(scanBasePackages={"package1", "package2"})
public class MyApplication { ... }
Currently this configuration extends the packages scanned by the @ComponentScan defined on @SpringBootApplication. However if one also has entities or Spring Data repositories in those packages one needs to add additional annotations for configuration.
@SpringBootApplication(scanBasePackages={"package1", "package2"})
@EntityScan(basePackages = {"package1", "package2"})
@EnableJpaRepositories(basePackages = {"package1", "package2"}))
public class MyApplication { ... }
Depending on what is used in the application there might be more configuration needed and it might also lead to disabling some auto-configuration I suspect (due to beans being loaded by the @Enable* annotations).
It would be a nice extension if the scanBasePackages could be extended to all other scanning parts as well. There is the @AutoConfigurationPackage annotation which provides this support for starters/auto-configuration. Couldn't the @SpringBootApplication annotation benefit from this as well and be an improvement on the user experience?
Comment From: snicoll
There is the @AutoConfigurationPackage annotation which provides this support for starters/auto-configuration.
What makes you think this is specific to starters or auto-configuration?
See also https://github.com/spring-projects/spring-boot/issues/27549
Comment From: mdeinum
Actually, I don't know, that was an (apparently) false assumption. Now there was a thing about assumptions.
Now I'm also calling my sanity as apparently I already registered the same issue and I didn't look (sorry for that, Monday and I need more coffee).
Nonetheless, I think that scanning multiple packages might have a bit more love. Currently, it needs putting more annotations on to configure it, but I also realize that there might be consequences that I don't oversee.
I did a small test locally and at first sight, it appears to be running fine when I add @AutoConfigurePackage to the @SpringBootApplication annotation and change the aliases from @ComponentScan to @AutoConfigurePackage. At least ./gradlew check runs without problems.
Comment From: snicoll
Let's close this one in favor of the documentation.