`
zhongzhihua
  • 浏览: 310415 次
  • 来自: ...
社区版块
存档分类
最新评论

autowire异常的三个情况

阅读更多
2010-3-11 16:06:00 net.sf.ehcache.config.ConfigurationFactory parseConfiguration
警告: No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/E:/kenny/%e9%a1%b9%e7%9b%ae/shops/webapp/WEB-INF/lib/ehcache-1.6.1.jar!/ehcache-failsafe.xml
2010-03-11 16:06:02,734 [main] ERROR [org.springframework.test.context.TestContextManager] - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1292d12] to prepare test instance [com.zzh.entity.product.BrandServiceImplTest@4e32c8]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.zzh.entity.product.BrandServiceImplTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.zzh.service.product.impl.BrandServiceImpl com.zzh.entity.product.BrandServiceImplTest.brandServiceImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brandServiceImpl': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.zzh.dao.product.BrandDao com.zzh.service.product.impl.BrandServiceImpl.brandDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.zzh.dao.product.BrandDao] is defined: Unsatisfied dependency of type [class com.zzh.dao.product.BrandDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.zzh.service.product.impl.BrandServiceImpl com.zzh.entity.product.BrandServiceImplTest.brandServiceImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brandServiceImpl': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.zzh.dao.product.BrandDao com.zzh.service.product.impl.BrandServiceImpl.brandDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.zzh.dao.product.BrandDao] is defined: Unsatisfied dependency of type [class com.zzh.dao.product.BrandDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
	at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
	... 19 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brandServiceImpl': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.zzh.dao.product.BrandDao com.zzh.service.product.impl.BrandServiceImpl.brandDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.zzh.dao.product.BrandDao] is defined: Unsatisfied dependency of type [class com.zzh.dao.product.BrandDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:671)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:610)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
	... 21 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.zzh.dao.product.BrandDao com.zzh.service.product.impl.BrandServiceImpl.brandDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.zzh.dao.product.BrandDao] is defined: Unsatisfied dependency of type [class com.zzh.dao.product.BrandDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
	at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
	... 34 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.zzh.dao.product.BrandDao] is defined: Unsatisfied dependency of type [class com.zzh.dao.product.BrandDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
	... 36 more

 autowire异常主要由三个情况发生的

像上面的情况是BrandDao没有注入,


1。你的BrandServiceImpl必须以@Service或@Component注解才行。

 

2。自动写入的时候把接口写成实现类了
@Autowired
private BrandServiceImpl      brandServiceImpl;
应该是
@Autowired
private BrandService    brandService ;

 

3.在BrandDao 类上加上@Repository注解

分享到:
评论
10 楼 羊肉小笼包 2017-10-30  
非常感谢,用第三种方式解决了service实现类里面装载不了dao接口
9 楼 Rainyn 2017-06-28  
碰到了另外一种情况。代码里有地方用到了dubbo的Service 引入的时候没注意 自动引成了com.alibaba.dubbo.config.annotation.Service;
应该引的是
org.springframework.stereotype.Service
8 楼 tsinglongwu 2016-09-13  
2.3都碰到过。但是今天发现一个新原因,就是mybatis忘了mapper注册。。。
7 楼 cuqing 2015-08-13  
@Service
public class RacePlanDaoService extends EntityService<RacePlan, Long> {
    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        initDao(sessionFactory, RacePlan.class);
    }
}
6 楼 星丨澈 2015-07-27  
帮了大忙了,用第三种方法解决了
5 楼 往事也加 2015-07-03  
大神能够解决这个问题不呢?http://zhidao.baidu.com/question/1959550835681904940.html&fr=none#
4 楼 南蛮虫 2013-09-24  
特意登录来感谢,困扰了一天
3 楼 ywlli 2012-12-24  
问题解决   很快乐
2 楼 key87 2010-08-30  
嘿嘿,问题解决了
1 楼 woxiangbo 2010-06-13  
三种情况我都尝试了,还是不行啊
QQ:273040644
恳请交流

相关推荐

Global site tag (gtag.js) - Google Analytics