Search code examples
javaspringmigrationjavabeansspring-4

Spring3 to Spring4 migration, No qualifying bean of type DtoMapper found for dependency


I am migrating a working project from Spring3 to Spring4, I only changed the pom.xml and now (on deploy) the application cannot find any beans of my mappers at runtime. Weird thing is, all my integration and unit tests still work. (and I test my mappers there)

I have checked multiple possible combinations of @Component("languagesDtoMapper") and @Qualifier("languagesDtoMapper"). I have tried to declare the bean in my spring configuration

<bean id="languagesDtoMapper" class="com.project.mapper.LanguagesDtoMapper"/>

But whatever I do, I keep getting the following error message:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.project.infrastructure.dtomapper.DtoMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=languagesDtoMapper)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)

Structure of the project:

in the controller :

 @Autowired
 @Qualifier("languagesDtoMapper")
 private DtoMapper<Languages, LanguagesTableDto> partnerLanguageDtoMapper;

Classes:

@Component("languagesDtoMapper")
public class LanguagesDtoMapper extends AbstractDtoMapper<Languages, LanguagesDto> {

public abstract class AbstractDtoMapper<Source, Target> implements DtoMapper<Source, Target> {

public interface DtoMapper<Source, Target> {

<context:annotation-config />
<context:component-scan base-package="com.project"/>

Any ideas are welcome


Solution

  • Found the issue after a 3 day search!

    Spring 3 will autowire beans on the ClassName, Spring 4 does this on the full.package.name.ClassName.

    In the controller I was calling

     import a.b.c.Languages;
     import a.b.c.LanguagesTableDto;
    
     @Autowired
     @Qualifier("languagesDtoMapper")
     private DtoMapper<Languages, LanguagesTableDto> partnerLanguageDtoMapper;
    

    and in my mapper was this :

     import x.y.z.Languages;
     import x.y.z.LanguagesTableDto;
    
     @Component("languagesDtoMapper")
     public class LanguagesDtoMapper extends AbstractDtoMapper<Languages, LanguagesDto> {
    

    The a.b.c.Languages was an interface, and unfortnuately the implementation, x.y.z.Languages is called the same. Making it very hard to find the issue.