【发布时间】:2017-05-21 13:25:14
【问题描述】:
几天来,我正在尝试创建 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> {
}
我查看了很多类似的问题,但没有一个答案对我有帮助。
【问题讨论】:
-
ClientRepository的实现类在哪里? -
@Arpit 我只在 ClientServiceImpl 中创建了一个 ClientRepository 的实例。这是我第一次体验这个 Spring Data JPA,我不知道如何正确操作。所以我按照例子
-
@Arpit 请看我在github上的项目(我加链接)
标签: java spring spring-mvc