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

Time:11-16

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

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

CodePudding user response:

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.

CodePudding user response:

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.

  • Related