UnsatisfiedDependencyException:使用name创建bean时出错

几天来,我正在尝试创建Spring CRUD应用程序。 我很困惑。 我无法解决这个错误。

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为’clientController’的bean时出错:通过方法’setClientService’参数0表示不满意的依赖关系; 嵌套exception是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为’clientService’的bean时出错:通过字段’clientRepository’表示的不满意的依赖关系; 嵌套exception是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有’com.kopylov.repository.ClientRepository’类型的限定bean可用:预期至少有1个bean可以作为autowire候选者。 依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

和这个

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为’clientService’的bean时出错:通过字段’clientRepository’表示的不满意的依赖关系; 嵌套exception是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有’com.kopylov.repository.ClientRepository’类型的限定bean可用:预期至少有1个bean可以作为autowire候选者。 依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

ClientController

@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 { } 

我查看了很多类似的问题,但没有人回答他们无法帮助我。

ClientRepository应使用@Repository标记进行注释。 使用您当前的配置,Spring将不会扫描该类并了解它。 在启动和连接的那一刻将找不到ClientRepository类。

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

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

此外,正如Bruno所提到的, ClientController不需要@Qualifier ,因为您只有一个服务实现。

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

考虑通过XML配置或基于注释的配置正确设置包扫描。

您还需要在ClientRepository实现上使用ClientRepository ,以允许Spring在@Autowired使用它。 由于它不在这里,我们只能假设这是缺少的。

作为旁注,如果setter方法仅用于@Autowired ,将@Autowired / @Qualifier直接放在您的成员上会更清晰。

 @Autowired @Qualifier("clientRepository") private ClientRepository clientRepository; 

最后,你不需要@Qualifier只有一个实现bean定义的类,所以除非你有多个ClientServiceClientRepository实现,你可以删除@Qualifier

尝试在主类的顶部添加@EntityScan(basePackages =“在此处插入包名称”)。

将@Repository注释添加到Spring Data JPA repo

根据文档,您应该设置XML配置:

  

这可能是因为你使用的pojos缺乏服务所需的精确构造函数。 也就是说,尝试生成serviceClient使用的pojo或对象(模型对象)的所有构造函数,以便客户端可以正确实例化。 在您的情况下,为您的客户端对象重新生成构造函数(带参数)(taht是您的模型对象)。

我有完全相同的问题,堆栈跟踪非常长。 在跟踪结束时我看到了这个:

InvalidQueryException:Keyspace’mykeyspace’不存在

我在cassandra中创建了键空间,并解决了这个问题。

 CREATE KEYSPACE mykeyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }; 

我遇到了同样的问题,因为我错过了用实体注释标记我的DAO类。 我试过下面的错误得到了解决。

 /** *`enter code here` */ @Entity <-- was missing earlier public class Topic { @Id String id; String name; String desc; . . . } 

查看Client表的表结构,如果db中的表结构与实体不匹配,则会出现此错误。

我有这个错误,这是因为db表和实体之间的主键数据类型不匹配…

我刚刚将@EnableJpaRepositories注释添加到Repository接口,将@EnableJpaRepositories (“domain.repositroy-package”)添加到主类。 它运作得很好。

应用程序需要放在与扫描包相同的目录中:

在此处输入图像描述