本文介绍了UnsatisfiedDependencyException:使用名称创建 bean 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For several days I'm trying to create Spring CRUD application. I'm confused.I can't solve this errors.

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为clientController"的 bean 时出错:通过方法setClientService"参数 0 表达的不满意依赖;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为clientService"的 bean 时出错:通过字段clientRepository"表达的不满意的依赖;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的com.kopylov.repository.ClientRepository"类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

还有这个

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为clientService"的 bean 时出错:通过字段clientRepository"表达的不满意依赖;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的com.kopylov.repository.ClientRepository"类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选.依赖注解:{@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);
}
}

ClientRepository

public interface ClientRepository extends JpaRepository<Client, Integer> {

}

我浏览了很多类似的问题,但没有人能帮到我.

I looked through a lot of similar questions, but no one answer to them can't help me.

推荐答案

ClientRepository 应该使用 @Repository 标签进行注释.使用您当前的配置,Spring 不会扫描类并了解它.在启动和接线的时候不会找到 ClientRepository 类.

The ClientRepository should be annotated with @Repository tag.With your current configuration Spring will not scan the class and have knowledge about it. At the moment of booting and wiring will not find the ClientRepository class.

编辑如果添加 @Repository 标记没有帮助,那么我认为问题现在可能出在 ClientServiceClientServiceImpl 上.

EDITIf adding the @Repository tag doesn't help, then I think that the problem might be now with the ClientService and ClientServiceImpl.

尝试使用 @Service 注释 ClientService(接口).由于您的服务应该只有一个实现,因此您无需使用可选参数 @Service("clientService") 指定名称.Spring 将根据接口的名称自动生成它.

Try to annotate the ClientService (interface) with @Service. As you should only have a single implementation for your service, you don't need to specify a name with the optional parameter @Service("clientService"). Spring will autogenerate it based on the interface' name.

此外,正如布鲁诺所提到的,ClientController 中不需要 @Qualifier,因为您只有一个服务实现.

Also, as Bruno mentioned, the @Qualifier is not needed in the ClientController as you only have a single implementation for the service.

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";
    }
}

这篇关于UnsatisfiedDependencyException:使用名称创建 bean 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:18