Suzan Cioc
Suzan Cioc

Reputation: 30097

How to scan both current and some other package with @ComponentScan?

If I write only

@ComponentScan

Spring will scan the current package and below.

If I write

@ComponentScan(basePackages = {"my.package.com"})

Can I specify current package implicitly along with some explicit packages?

Something like this

@ComponentScan(basePackages = {"", "my.package.com"})

Upvotes: 1

Views: 1272

Answers (1)

Sergii Bishyr
Sergii Bishyr

Reputation: 8641

The JavaDoc says:

If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

So if you declare any package to be scan the default behavior will be overridden. However I cannot see the reason why you need to have implicit scan for current package as long as it is not something that will be changed in runtime. If you want to protected yourself in case of refactoring, you can use Type-safe basePackageClasses instead:

@ComponentScan(basePackageClasses = {Config.class, ClassInPackageToScan.class})

Upvotes: 2

Related Questions