小编典典

UnsatisfiedDependencyException:创建名称为bean的错误

spring-mvc

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

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

客户资料库

public interface ClientRepository extends JpaRepository<Client, Integer> {

}

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


阅读 1269

收藏
2020-06-01

共1个答案

小编典典

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";
    }
}
2020-06-01