spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Document how to customize the basePackages that auto-configurations consider (for example Spring Data Repositories)

Open mdeinum opened this issue 4 years ago • 3 comments

When adding an external library to Spring Boot with a different base package structure than your Spring Boot based application, a pattern emerges in the form of additional annotations to detect components, entities, repositories etc.

@SpringBootApplication(scanBasePackages= { "package1", "package2",...})
@EntityScan("package1", "package2",...})
@EnableJpaRepositories("package1", "package2", ...})
public class FooApplication { ... }

Or maybe even all packages added through an @ComponentScan to be included (unless explicitly specified that is).

This could be reduced if the scanBasePackages property was taken into account in places like the @EntityScan, auto- configuration of Spring Data Repositories etc.

mdeinum avatar Aug 03 '21 14:08 mdeinum

That's what AutoConfigurationPackage is for but it would deserve a bit more love in the doc.

snicoll avatar Aug 04 '21 06:08 snicoll

Now that is an annotation that I didn't already know (and it already exists since Spring Boot 1.3). This has been a well-kept secret. I might take the time and write a small blog post on it.

mdeinum avatar Aug 05 '21 05:08 mdeinum

We should update @EntityScan at the same time to make it clear that using it will result in @AutoConfigurationPackage being ignored.

wilkinsona avatar May 25 '22 15:05 wilkinsona

We should update @EntityScan at the same time to make it clear that using it will result in @AutoConfigurationPackage being ignored.

Yes, just for reference: this has been already discussed in https://github.com/spring-projects/spring-boot/issues/19024 too.

pnavato avatar Feb 07 '23 10:02 pnavato