【问题标题】:No qualifying bean of type found for dependency没有为依赖找到符合条件的 bean
【发布时间】:2017-01-16 19:56:19
【问题描述】:

我很想运行一个 JUnit 测试,嗯,我的 spring boot 项目是这样的:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.br.suppcomm.ocp.entity.Login;


public interface LoginDao extends JpaRepository<Login, Long>{
...
}

服务:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.br.suppcomm.ocp.dao.CompanyDAO;
import com.br.suppcomm.ocp.dao.LoginDao;
import com.br.suppcomm.ocp.entity.Login;



@Service
public class LoginService  {

    @Autowired LoginDao loginDao; 


    @Autowired CompanyDAO companyDao;


    public void save(Login login) {

        loginDao.save(login);


    }


    public void delete(Login login) {
         loginDao.delete(login);
    }


    public Login findById(Login login) {
        return loginDao.findOne(login.getLoginId());
    }


    public Login findByEmail(Login login) {
        return loginDao.findByEmail(login.getEmail());
    }


    public Login FindByLogin(Login login) {
        return loginDao.FindByLogin(login);
    }


    public List<Login> getAll() {

        return loginDao.findAll();
    }


    public Login getUserAccessLoginPass(String login, String password) {
        return loginDao.getUserAccessLoginPass(login, password);
    }


    public void update(Login login) {
         loginDao.save(login);

    }

}

测试:

package com.example;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.br.suppcomm.ocp.service.LoginService;


@RunWith(SpringRunner.class)

@SpringBootTest
public class OcpJpaApplicationTests {

    @Autowired LoginService loginService;

    @Test
    public void contextLoads() {


    }    

}

当我运行这段代码时,确实显示了以下错误。

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:        

创建名为“loginService”的 bean 时出错:不满足的依赖关系 通过字段'loginDao'表示:没有类型的限定bean

[com.br.suppcomm.ocp.dao.LoginDao] 找到依赖项 [com.br.suppcomm.ocp.dao.LoginDao]:预计至少有 1 个 bean 有资格成为此依赖项的自动装配候选者。依赖 注释: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 找到类型为 [com.br.suppcomm.ocp.dao.LoginDao] 的合格 bean 依赖项 [com.br.suppcomm.ocp.dao.LoginDao]:预计至少为 1 有资格作为此依赖项的自动装配候选者的 bean。 依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

不知道怎么回事!!请。

【问题讨论】:

  • 你的课程在哪些包中?
  • 对于初学者,您应该让我们看看您的主要课程...您的注释全都丢失了...您的存储库需要一个@Repository。您需要正确注释您的测试类,如下所示:@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) 更准确的答案,我们需要查看您的主类...

标签: spring spring-boot autowired


【解决方案1】:

添加@Repository 你的接口
注解,使其可以自动装配。

@Repository
public interface LoginDao extends JpaRepository<Login, Long>{

}

它会这样工作! Exception 表示 SPring 无法找到限定符,以便自动装配您需要对其进行定型化的东西。

【讨论】:

    【解决方案2】:

    请将classes 属性添加到您的@SpringBootTest 注释中,如下所示:

    @SpringBootTest(classes = { Application.class })

    这样它就会知道它必须执行您在 Application 类中指定的组件扫描等。

    【讨论】:

    • 没用。我把这段代码放在 Application.class 中有 componentScan 注释。
    【解决方案3】:

    在LoginDao上添加@Repository注解

    【讨论】:

    • 我们需要最后一个缺失的部分的第三个答案!有志愿的吗。。? :-)
    • 尝试在 LoginDao 上使用 @ContextConfiguration(classes = {Application.class}, loader = SpringApplicationContextLoader.class) 代替 @SpringBootTest@Repository
    • @RunWith(SpringRunner.class) @SpringBootTest(classes = { OcpJpaApplication.class }) @ContextConfiguration(classes = {OcpJpaApplication.class}, loader = SpringApplicationContextLoader.class) 公共类 OcpJpaApplicationTests { ---没用
    • 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项[com.br.suppcomm.ocp]找到类型[com.br.suppcomm.ocp.dao.LoginDao]的合格bean .dao.LoginDao]:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
    • SpringApplicationContextLoader 被贬低了!
    【解决方案4】:

    您需要将此注释添加到您的测试中:

    @DataJpaTest

    这将导致您的应用程序的持久性切片被初始化。

    【讨论】:

      猜你喜欢
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 2013-06-28
      • 2014-09-24
      • 1970-01-01
      • 2017-10-01
      相关资源
      最近更新 更多