Wildcards in a base package attribute when scanning Spring components

Can someone explain what ** means in the context of spring configuration?

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

and how is this different from

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

I could not find anything about how to use the / ant template paths in the base package attribute of the check component element.

Could you also point me to any documentation / source code that would explain the use of wildcards in the attribute of a scan component? My google-fu is useless

EDIT: I did a few more experiments based on the accepted answer, now it all makes sense to know how the attribute value of the base package is "converted" to the resource string.

So, I created two managed components of spring

 abSpringBean2 abcdSpringBean1 

SpringBean1 uses SpringBean2 using @Autowired

therefore not only this:

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

and this:

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

work fine in the sense that SpringBean2 may be correctly allowed for input in SpringBean1, but they will also work:

 <context:component-scan base-package="ab**.**.**"/> <!-- as many .** as you want--> <context:component-scan base-package="ab**"/> <context:component-scan base-package="ab*"/> 

This will result in an error with NoSuchBeanDefinitionException due to an unresolved type of SpringBean2:

 <context:component-scan base-package="ab*"/> 
+4
source share
2 answers

Both mean the same thing, ultimately, the name of the base package, for example ab , is converted to a resource search with this kind of resource name - classpath*:/a/b/**/*.class , and your first base package name will be have this resource type: t22>, both will do the same, getting all the class files under the package name ab.

+10
source

A more powerful way to determine what to scan and enable / exclude is described in the spring reference documents. For example, you can do this:

 <context:component-scan base-package="org.example"> <context:include-filter type="regex" expression=".*Stub.*Repository"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> 

See section 3.10.3 spring 3.0 ref docs .

You can also do:

 <context:component-scan base-package="org.*.example"/> 
+4
source

All Articles