multiple packages in context:component-scan, spring config
Asked Answered
R

8

196

How can I add multiple packages in spring-servlet.xml file in context:component-scan element?

I have tried

<context:component-scan base-package="z.y.z.service" base-package="x.y.z.controller" />

and

<context:component-scan base-package="x.y.z.service, x.y.z.controller" />

and

<context:component-scan base-package="x.y.z.service" />
<context:component-scan base-package="x.y.z.controller" />

but got error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [x.y.z.dao.daoservice.LoginDAO] found for dependency:
Reggy answered 11/3, 2011 at 5:50 Comment(0)
P
294

The following approach is correct:

<context:component-scan base-package="x.y.z.service, x.y.z.controller" /> 

Note that the error complains about x.y.z.dao.daoservice.LoginDAO, which is not in the packages mentioned above, perhaps you forgot to add it:

<context:component-scan base-package="x.y.z.service, x.y.z.controller, x.y.z.dao" /> 
Parrisch answered 11/3, 2011 at 9:37 Comment(1)
This answer implies that there is NOT recursion for the members of base-package BUT there IS: #7774795 . I would suggest altering the answer a little to make this clear.Tinkle
A
57

Annotation Approach

@ComponentScan({ "x.y.z", "x.y.z.dao" })
Ascarid answered 28/3, 2014 at 16:21 Comment(3)
Do we compulsorily need to add specific packages or it can scan sub packages automatically if we only define the top level package?Wyman
@NikhilSahu nope, it's a recursive scan ( Spring 3 > )Ascarid
SO link for more info: #10795087Cyrilcyrill
C
46

You can add multiple base packages (see axtavt's answer), but you can also filter what's scanned inside the base package:

<context:component-scan base-package="x.y.z">
   <context:include-filter type="regex" expression="(service|controller)\..*"/>
</context:component-scan>
Cultivator answered 11/3, 2011 at 6:3 Comment(3)
@shams no need to sir me, but if the answer is correct you should mark it as accepted (click the checkmark)Cultivator
Why can't you add multiple base packages? Like for instance "org.example, com.example"?Winou
@Shervin you can. exactly as you wrote. "Alternatively, you can specify a comma-separated list that includes the parent package of each class." (also see the expected answer)Cultivator
L
20
<context:component-scan base-package="x.y.z"/>

will work since the rest of the packages are sub packages of "x.y.z". Thus, you dont need to mention each package individually.

Libre answered 24/11, 2011 at 0:6 Comment(4)
Ok! Maybe it was another reason, but this just didn't work for me with spring mvc for portlets...Videlicet
@Videlicet must've been through other reasons, since this is well documented and advertised featureLeatriceleave
Well, I can't say the opposite as many people are correcting me. I wish I could find why this didn't work for me... Thanks anyway!Videlicet
Does it holds true even for the annotation based configuration?Wyman
C
6

Another general Annotation approach:

@ComponentScan(basePackages = {"x.y.z"})
Corbicula answered 6/7, 2016 at 11:12 Comment(0)
J
5

A delayed response but to give multiple packages using annotation based approach we can use as below:

@ComponentScan({"com.my.package.one","com.my.package.subpackage.two","com.your.package.supersubpackage.two"})

Joachim answered 5/12, 2019 at 17:33 Comment(0)
C
2

If x.y.z is the common package then you can use:

<context:component-scan base-package="x.y.z.*">

it will include all the package that is start with x.y.z like: x.y.z.controller,x.y.z.service etc.

Cf answered 16/12, 2016 at 4:45 Comment(0)
B
1

For Example you have the package "com.abc" and you have multiple packages inside it, You can use like

@ComponentScan("com.abc")
Butterandeggs answered 8/2, 2017 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.