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

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

2 Answers2

1

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.

Gaël J
  • 11,274
  • 4
  • 17
  • 32
0

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.

Utsav10
  • 400
  • 2
  • 11
  • 1
    I think your answer is slightly confusing: `component-scan` itself isn't responsible to instantiate beans but it will lead to beans of the packages and subpackages being instantiated if needed. – Gaël J Nov 15 '21 at 20:42