Using the basePackageClasses attribute in ComponentScan

When using component scan, we recommend that you use the basePackageClasses attribute. The following snippet shows an example:

    @ComponentScan(basePackageClasses = ApplicationController.class)     public class SomeApplication {

The basePackageClasses attribute is the type-safe alternative to basePackages() in order to specify the packages to scan for the annotated components. The package of each specified class will be scanned.

This will ensure that even when the package is renamed or moved, the component scan would work as expected.

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.