195

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:
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Shams
  • 3,637
  • 5
  • 31
  • 49

8 Answers8

293

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" /> 
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 5
    This answer implies that there is NOT recursion for the members of base-package BUT there IS: http://stackoverflow.com/questions/7774295/spring-xml-file-configuration-hierarchy-help-explanation/7774597#7774597 . I would suggest altering the answer a little to make this clear. – djangofan Oct 27 '15 at 17:59
57

Annotation Approach

@ComponentScan({ "x.y.z", "x.y.z.dao" })
biology.info
  • 3,500
  • 2
  • 28
  • 39
  • Do we compulsorily need to add specific packages or it can scan sub packages automatically if we only define the top level package? – Nikhil Sahu Jun 17 '16 at 13:52
  • 1
    @NikhilSahu nope, it's a recursive scan ( Spring 3 > ) – biology.info Jun 17 '16 at 14:38
  • SO link for more info: https://stackoverflow.com/questions/10794587/how-to-scan-multiple-paths-using-the-componentscan-annotation/21333294 – dkb Mar 16 '18 at 14:32
45

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>
Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 2
    @shams no need to sir me, but if the answer is correct you should mark it as accepted (click the checkmark) – Sean Patrick Floyd Mar 11 '11 at 06:57
  • 1
    Why can't you add multiple base packages? Like for instance "org.example, com.example"? – Shervin Asgari Jan 17 '14 at 13:12
  • @Shervin you can. exactly as you wrote. ["Alternatively, you can specify a comma-separated list that includes the parent package of each class."](http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-scanning-autodetection) (also see the expected answer) – Sean Patrick Floyd Jan 17 '14 at 14:44
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.

Sean Connolly
  • 5,692
  • 7
  • 37
  • 74
Irene
  • 379
  • 1
  • 5
  • 9
  • 2
    Ok! Maybe it was another reason, but this just didn't work for me with spring mvc for portlets... – elcadro Apr 29 '13 at 06:27
  • @elcadro must've been through other reasons, since this is well documented and advertised feature – eis Nov 23 '13 at 07:55
  • 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! – elcadro Nov 26 '13 at 12:49
  • Does it holds true even for the annotation based configuration? – Nikhil Sahu Jun 17 '16 at 14:00
6

Another general Annotation approach:

@ComponentScan(basePackages = {"x.y.z"})
Matt
  • 9,068
  • 12
  • 64
  • 84
Robocide
  • 6,353
  • 4
  • 37
  • 41
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"})

Brooklyn99
  • 987
  • 13
  • 24
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.

Amit Sharma
  • 71
  • 1
  • 2
1

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

@ComponentScan("com.abc")
Matt
  • 9,068
  • 12
  • 64
  • 84
sForSujit
  • 987
  • 1
  • 10
  • 24