开发者

Can't get@Autowired to work with beans

I'm currently just starting out with Spring and trying to get the hang of it. But I've run into a problem: My @Autowired keeps failing.

In my spring.xml I've got this:

<!--Handle @Autowired-->
<context:annotation-config />
<context:component-scan  base-package="org.MYPROJECT">
    <context:include-filter type="regex" expression=".*"/>
</context:component-scan>

When running some tests it fails with this (I'm only showing the last exception as its the most important)

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.quackbot.dao.AdminDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
    ... 50 more

So I try to fix it by adding the bean manually to the config

<bean id="AdminDAO" class="org.quackbot.dao.hibernate.AdminDAOHibernate">
</bean>

Run it again, now I get this

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.quackbot.dao.AdminDAO] is defined: expected single matching bean but found 2: [adminDAOHibernate, AdminDAO]
    at org.springframework.beans.factory.support.D开发者_开发知识库efaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:796)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
    ... 50 more

Seems I just can't win when using Spring... First it complains that the beans don't exist, when specifying them it complains that there's too many. What's going on? Why can't it load the correct beans?


Do you have an annotation in your dao?

Something like @Repository or @Component right above the class to tell Spring this needs to be picked up when scanning your base package?


This is more than enough, no other XML configuration is needed in your case:

<context:component-scan base-package="org.quackbot"/>

Secondly, make sure AdminDAOHibernate implements AdminDAO.

Last but not least, double check that the field annotated with @Autowired is of type AdminDAO (interface).

Last piece of advice: add default constructor to AdminDAOHibernate and place place some logging statement there or put breakpoint. There should be only one invocation of this constructor, although it gets tricky when class proxies are involved.


Is this a Spring MVC app? One way or another, I'm guessing you have two separate Spring contexts, whether you know it or not. In one context, you have a dependency on your AdminDAO, but it isn't available, which causes your first exception. Your other context also has a dependency on AdminDAO which is already satisfied, but when you manually add another AdminDAO bean, that fails because there are two of them.


@Autowired using annotations

@Repository("ExampleDao") public class ExampleDaoImpl implements ExampleDao

@Service("ServiceExample") public class ExampleServiceImpl implements ExampleService

@Controller public class ExampleController

@Autowired private ExmpleService ServiceExample;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜