@Autowired - No qualifying bean of type found for dependency

前端 未结 17 1550
自闭症患者
自闭症患者 2020-12-07 15:00

I\'ve started my project by creating entities, services and JUnit tests for services using Spring and Hibernate. All of this works great. Then I\'ve added spring-mvc to make

相关标签:
17条回答
  • 2020-12-07 15:27

    Can you try annotating only your concrete implementation with @Component? Maybe the following answer could help. It is kind of a similar problem. I usually put Spring annotations in the implementation classes.

    https://stackoverflow.com/a/10322456/2619091

    0 讨论(0)
  • 2020-12-07 15:27

    My guess is that here

    <context:component-scan base-package="pl.com.radzikowski.webmail" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    

    all annotations are first disabled by use-default-filters="false" and then only @Controller annotation enabled. Thus, your @Component annotation is not enabled.

    0 讨论(0)
  • 2020-12-07 15:31

    You should autowire interface AbstractManager instead of class MailManager. If you have different implemetations of AbstractManager you can write @Component("mailService") and then @Autowired @Qualifier("mailService") combination to autowire specific class.

    This is due to the fact that Spring creates and uses proxy objects based on the interfaces.

    0 讨论(0)
  • 2020-12-07 15:34

    I was facing the same issue while auto-wiring the class from one of my jar file. I fixed the issue by using @Lazy annotation:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Lazy;
    
        @Autowired
        @Lazy
        private IGalaxyCommand iGalaxyCommand;
    
    
    0 讨论(0)
  • 2020-12-07 15:36

    Correct way shall be to autowire AbstractManager, as Max suggested, but this should work fine as well.

    @Autowired
    @Qualifier(value="mailService")
    public MailManager mailManager;
    

    and

    @Component("mailService")
    @Transactional
    public class MailManager extends AbstractManager {
    }
    
    0 讨论(0)
  • 2020-12-07 15:36

    The solution that worked for me was to add all the relevant classes to the @ContextConfiguration annotation for the testing class.

    The class to test, MyClass.java, had two autowired components: AutowireA and AutowireB. Here is my fix.

    @ContextConfiguration(classes = {MyClass.class, AutowireA.class, AutowireB.class})
    public class MyClassTest {
    ...
    }
    
    0 讨论(0)
提交回复
热议问题