Ajay Sabarish
Ajay Sabarish

Reputation: 115

Spring : Does <context:component-scan /> instantiate all beans defined in the base package?

<context:component-scan base-package="packageA" />

Does this instantiate all beans defined in packageA.*(base package and sub-packages) ?

Upvotes: -1

Views: 431

Answers (2)

Ga&#235;l J
Ga&#235;l J

Reputation: 15275

If your question is "does it apply to subpackages automatically", then the answer is yes.

Beans in the base package and sub packages will be found by Spring when using component-scan or @ComponentScan.

Upvotes: 1

Utsav10
Utsav10

Reputation: 629

Spring will search every class within this package.

No, it will not instantiate the beans, but by component-scan you are telling your spring application about the packages where it should search for Components and Beans.

You can verify this by declaring beans in packageA.subpackage1.* and packageA.subpackage2.*

but put

<context:component-scan base-package="packageA.subpackage1" />

put some logging in your beans and you won't see any logs coming from bean methods declared in packageA.subpackage2.* package.

Upvotes: 0

Related Questions