Horace
Horace

Reputation: 151

What syntax for including sub-packages in <context:component-scan>?

I am using Spring and I have a long list of subpackages, do I have to specify them one by one in the <context:component-scan> tag?

<context:component-scan base-package="com.fooapp.mainpackage, 
com.fooapp.mainpackage.subpackage1, 
com.fooapp.mainpackage.subpackage2, 
com.fooapp.mainpackage.subpackage3" />

Upvotes: 15

Views: 17932

Answers (2)

danny.lesnik
danny.lesnik

Reputation: 18639

In addition I would to add that by default, classes annotated with @Component, @Repository, @Service, @Controller, or a custom annotation that itself is annotated with @Component are the only detected candidate components.

You can change this behavior by applying custom filters which are include-filter or exclude-filter

For example:

<context:component-scan base-package="com.vanilla">
      <context:exclude-filter 
                type="annotation"
                expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

It will exclude all @Repository annotations.

Upvotes: 5

skaffman
skaffman

Reputation: 403471

Component-scanning supports package hierarchies, so this should work:

<context:component-scan base-package="com.fooapp.mainpackage"/>

This is easy and quicker to verify for yourself - did you try it?

Upvotes: 25

Related Questions