注入自动连接的依赖项失败;没有匹配的bean

时间:2013-11-23 13:28:43

标签: java spring hibernate

我正在尝试使用Spring和Hibernate编写一个应用程序,而且我是一个完整的新手。无法摆脱此错误消息:

  

SEVERE:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例的异常   org.springframework.beans.factory.BeanCreationException:创建名为'commentService'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.fiskin.comm.server.dao.CommentDAO com.fiskin.comm.server.services.CommentServiceImpl.commentDAO;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[com.fiskin.comm.server.dao.CommentDAO]的匹配bean依赖:预期至少有1个bean符合此依赖关系的autowire候选资格。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

这是我的评论DAO:

package com.fiskin.comm.server.dao;
@Repository("commentDAO")
public class CommentDAO extends JpaDAO<Long, Comment> {

 @Autowired
 EntityManagerFactory entityManagerFactory;

 @PostConstruct
 public void init() {
  super.setEntityManagerFactory(entityManagerFactory);
 }

}

CommentServiceImpl:

    package com.fiskin.comm.server.services;


    @Service("commentService")
    public class CommentServiceImpl implements CommentService {

@Autowired
private CommentDAO commentDAO;

@PostConstruct 
 public void init() throws Exception { 
 } 

 @PreDestroy 
 public void destroy() { 
 } 

@Override
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
public void addComment(Date d, String t) {
    Comment c = new Comment(d, t);
    commentDAO.add(c);
}

@Override
public List<Comment> allComments() {

    return commentDAO.all();
}

@Override
public List<Comment> getCommentsByDate(Date from, Date to) {

    return commentDAO.getByDate(from, to);
}

}

和applicationContext的片段:

     <context:component-scan base-package="com.fiskin.comm" >
      <context:include-filter type="aspectj" expression="com.fiskin.comm.*" />
     </context:component-scan>

      <tx:annotation-driven />

      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
       <property name="persistenceUnitName" value="MyPersistenceUnit" />
      </bean>

      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
       <property name="entityManagerFactory" ref="entityManagerFactory" />
      </bean>

任何帮助都将不胜感激。

UPD:完成堆栈跟踪

ноя 23, 2013 4:21:28 PM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
ноя 23, 2013 4:21:30 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
ноя 23, 2013 4:21:30 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
ноя 23, 2013 4:21:30 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@100b6ba')
ноя 23, 2013 4:59:27 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextDestroyed()
ноя 23, 2013 4:59:27 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextDestroyed()
ноя 23, 2013 5:00:20 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
ноя 23, 2013 5:00:23 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'commentService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.fiskin.comm.server.dao.CommentDAO com.fiskin.comm.server.services.CommentServiceImpl.commentDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.fiskin.comm.server.dao.CommentDAO] 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.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:196)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:983)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1660)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.fiskin.comm.server.dao.CommentDAO com.fiskin.comm.server.services.CommentServiceImpl.commentDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.fiskin.comm.server.dao.CommentDAO] 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
    ... 27 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.fiskin.comm.server.dao.CommentDAO] 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:901)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:685)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    ... 29 more

0 个答案:

没有答案