Spring configuration wildcards oddity

Jun 12, 2010   #java  #spring 

When configuring Spring with wildcards by using @ContextConfiguration(locations ={"classpath:**/HelloWorld.xml"}) it may happen that the Spring container throws an exception like this even if the content of your configuration file is correct:

Exception: Error creating bean with name 'com.sample.SampleBean': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sample.OtherBean com.sample.SampleBean.otherBean; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.sample.OtherBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

In my case, the problem was that I misspelled the name of the Spring configuration file as HeloWorld.xml If I had not used any wildcards in the configuration annotation, Spring would have thrown a more explicit exception that would have told me that the configuration file could not be found.

But as soon as you use wildcards, Spring does not complain about a missing configuration file but simply does not know the beans defined in the file.