没有匹配的类型的bean ...找到依赖

时间:2012-01-22 13:07:19

标签: java spring hibernate spring-mvc autowired

经过几天尝试并在springource论坛上等待答案后,我会在这里试试。 运行我的应用程序会导致以下异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.example.my.services.user.UserService] 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)}
    org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Thread.java:662)

这是相关代码

应用程序上下文:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="org.postgresql.Driver" />
  <property name="url" value="jdbc:postgresql://localhost:5432/test" />
  <property name="username" value="test" />
  <property name="password" value="test" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan" value="com.example.my.entities.*" />
  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
  <property name="hibernateProperties">
    <props>
      <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
      <prop key="hibernate.show_sql">true</prop>
    </props>
  </property>
</bean>

<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>

com.example.my.entities.user:

@Entity
@Table( name = "tbl_users" )
public class User
{
  @Id
  @Column( name = "id" )
  @GeneratedValue
  private int id;

  @Column( name = "username" )
  private String username;

  @Column( name = "password" )
  private String password;

  public void setId( int id )
  {
    this.id = id;
  }

  public int getId()
  {
    return id;
  }

  public void setUsername( String username )
  {
    this.username = username;
  }

  public String getUsername()
  {
    return username;
  }

  public void setPassword( String password )
  {
    this.password = password;
  }

  public String getPassword()
  {
    return password;
  }
}

服务:

@Service
public class UserServiceImpl implements UserService
{
  @Autowired
  private UserDAO userDAO;

  @Override
  @Transactional
  public void addUser( User user )
  {
    userDAO.addUser( user );
  }

  @Override
  @Transactional
  public List<User> listUsers()
  {
    return userDAO.listUsers();
  }

  @Override
  @Transactional
  public void removeUser( int id )
  {
    userDAO.removeUser( id );
  }
}

8 个答案:

答案 0 :(得分:118)

多件事可能导致这种情况,我没有费心去检查你的整个存储库,所以我在这里出去了。

首先,如果您使用注释进行配置,则可能会从com.example.my.services.user.UserService的实现中缺少注释(@Service或@Component)。如果您正在使用(仅)xml,那么您可能缺少UserService实现的<bean> - 定义。

如果您正在使用注释并且实现已正确注释,请检查实施所在的包是否已扫描(请检查<context:component-scan base-package= - 值)。

答案 1 :(得分:13)

将此添加到applicationContext:

 <bean id="userService" class="com.example.my.services.user.UserServiceImpl ">

答案 2 :(得分:10)

将注释@Repository添加到userDao Class的头部。如果userDao是一个接口,请将此注释添加到接口的实现中。

答案 3 :(得分:6)

由于使用了AOP,我在测试配置中遇到了类似的问题。 我在spring-config.xml

中添加了这行代码
<aop:config proxy-target-class="true"/>

它有效!

答案 4 :(得分:1)

我遇到了类似的问题,但我从com.example.my.services.myUser.MyUserServiceImpl的实现中遗漏了(@Service或@Component)

答案 5 :(得分:1)

如果这只发生在部署中,请确保您具有在.war中引用的包的依赖关系。例如,这在我的机器上本地工作,调试配置工作正常,但在部署到Amazon的Elastic Beanstalk之后,我收到此错误并注意到其中一个依赖项未捆绑在.war包中。

答案 6 :(得分:1)

就我而言,这是CrudRepository的错误依赖。我的IDE还添加了以下内容:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>1.11.2.RELEASE</version>
    </dependency>

但我只是需要:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>RELEASE</version>
    </dependency>

我删除了第一个,一切都很好。

答案 7 :(得分:0)

我有同样的问题,但在我的情况下,实施的课程意外地变成了“抽象”,因此自动装配失败了。