当前位置: 首页 > 面试题库 >

UnsatisfiedDependencyException:使用名称创建bean时出错

相德宇
2023-03-14
问题内容

几天来,我一直在尝试创建Spring CRUD应用程序。我糊涂了。我无法解决此错误。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientController': Unsatisfied dependency expressed through method 'setClientService' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientService': Unsatisfied dependency expressed through field 'clientRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.kopylov.repository.ClientRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

还有这个

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientService': Unsatisfied dependency expressed through field 'clientRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.kopylov.repository.ClientRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

客户端控制器

@Controller
public class ClientController {
private ClientService clientService;

@Autowired
@Qualifier("clientService")
public void setClientService(ClientService clientService){
    this.clientService=clientService;
}
@RequestMapping(value = "registration/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute Client client){
    this.clientService.addClient(client);
return "home";
}
}

ClientServiceImpl

@Service("clientService")
public class ClientServiceImpl implements ClientService{

private ClientRepository clientRepository;

@Autowired
@Qualifier("clientRepository")
public void setClientRepository(ClientRepository clientRepository){
    this.clientRepository=clientRepository;
}



@Transactional
public void addClient(Client client){
    clientRepository.saveAndFlush(client);
}
}

客户资料库

public interface ClientRepository extends JpaRepository<Client, Integer> {

}

我浏览了许多类似的问题,但是没有人回答不能帮助我。


问题答案:

ClientRepository应该用@Repository标记注释。使用你当前的配置,Spring将不会扫描该类并对其有所了解。在启动和连接时,找不到ClientRepository类。

编辑 如果添加@Repository标签没有帮助,那么我认为问题可能出在ClientServiceand ClientServiceImpl

尝试用注释ClientService(接口)@Service。由于你应该只为服务提供一个实现,因此不需要使用optional参数指定名称@Service("clientService")。Spring将根据接口名称自动生成它。

另外,如Bruno所述,由于你只有该服务的单个实现,因此@Qualifier不需要ClientController

ClientService.java

@Service
public interface ClientService {

    void addClient(Client client);
}

ClientServiceImpl.java(选项1)

@Service
public class ClientServiceImpl implements ClientService{

    private ClientRepository clientRepository;

    @Autowired
    public void setClientRepository(ClientRepository clientRepository){
        this.clientRepository=clientRepository;
    }

    @Transactional
    public void addClient(Client client){
        clientRepository.saveAndFlush(client);
    }
}

ClientServiceImpl.java(选项2 /首选)

@Service
public class ClientServiceImpl implements ClientService{

    @Autowired
    private ClientRepository clientRepository;

    @Transactional
    public void addClient(Client client){
        clientRepository.saveAndFlush(client);
    }
}

ClientController.java

@Controller
public class ClientController {
    private ClientService clientService;

    @Autowired
    //@Qualifier("clientService")
    public void setClientService(ClientService clientService){
        this.clientService=clientService;
    }

    @RequestMapping(value = "registration", method = RequestMethod.GET)
    public String reg(Model model){
        model.addAttribute("client", new Client());
        return "registration";
    }

    @RequestMapping(value = "registration/add", method = RequestMethod.POST)
    public String addUser(@ModelAttribute Client client){
        this.clientService.addClient(client);
    return "home";
    }
}


 类似资料:
  • 当我尝试启动应用程序时,我得到以下消息: “不满意的依赖关系”异常:创建名称为“产品服务”的 Bean 时出错 [C:\Users\Acasa\0 SDA\0 Proiecte 实践\attentive2细节\目标\类\com\示例\attentive2细节\服务\产品服务.class]:通过构造函数参数 0 表示的不满意的依赖关系;嵌套的异常是组织.springframework.bean.fa

  • 还有这个 UnsatisfiedDependencyException:创建名为“Client Service”的bean时出错:通过字段“Client Repository”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.nosuchbeandefinitionexception:没有“com.kopylov.repository.clien

  • 我正在尝试使用spring boot和JPA实现一个服务器。 模型类: 存储库: 资源:

  • 问题内容: 我将所有的XML Spring配置都转换为Java代码配置,但是由于我有一个丑陋的异常,所以我无法运行我的所有测试(它们之前都曾进行过测试): 这是我的测试课: 和我的: 我在测试课上尝试了推子,但这没用。该项目是Spring MVC项目,但是现在我仅测试服务层,所以为什么要在我的课程上放置该注释?即使有了该注释,也会出现另一个错误。那么,这里发生了什么? 问题答案: 您的配置很好,除

  • 问题内容: 我正在使用Springframework和Tomcat创建一个简单的REST服务。响应应该像{“ id”:“ 101”,“ name”:“ Ram”}之类的json中。每次运行时,出现以下错误 从这里开始,以下是我的Java代码和xml文件。 POM.xml web.xml front-controller-servlet.xml json / User.java json / Use