【问题标题】:UnsatisfiedDependencyException: Error creating bean with nameUnsatisfiedDependencyException:创建具有名称的 bean 时出错
【发布时间】: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


【解决方案1】:

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

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

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

此外,正如布鲁诺所提到的,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";
    }
}

【讨论】:

  • 现在我有这样的错误:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.kopylov.repository.ClientRepository”类型的合格bean可用:预计至少有1个有资格作为自动装配候选者的bean .依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
  • 我也遇到了同样的问题,忘记在impl中添加Service注解了。添加服务注释解决了这个问题。谢谢@Alex
【解决方案2】:

我知道这似乎为时已晚,但将来可能会对其他人有所帮助。

我遇到了同样的错误,问题是spring boot没有读取我的服务包所以添加:

@ComponentScan(basePackages = {"com.example.demo.Services"})(你必须指定你自己的服务包路径)和类demoApplication(具有main函数的类)和服务接口必须注解@Service和实现服务的类接口必须用@Component注解,然后自动装配服务接口。

【讨论】:

  • 能不能把它说得更清楚明白
【解决方案3】:

尝试在主类顶部添加 @EntityScan(basePackages = "insert package name")。

【讨论】:

  • 我没有主课怎么办?
【解决方案4】:

如果你使用的是 Spring Boot,你的主应用应该是这样的(只是为了简单地制作和理解事物) -

package aaa.bbb.ccc;
@SpringBootApplication
@ComponentScan({ "aaa.bbb.ccc.*" })
public class Application {
.....

确保您已对 @Repository 和 @Service 进行了适当注释。

确保您的所有软件包都属于 - aaa.bbb.ccc.*

在大多数情况下,此设置可以解决这类琐碎问题。 Here 是一个完整的例子。希望对您有所帮助。

【讨论】:

  • 这个固定的地雷。多谢。但我想知道为什么即使它们存在,它也不会自动检测到我的 bean/类。这是某种错误吗?无论如何,我的应用程序都在一个包下,我没有任何其他包。我不明白为什么我必须手动声明组件类。
  • 好吧,如果您使用的是@SPringbootApplication 注解,那么您不需要@ComponentScan。 @SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiguration
【解决方案5】:

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

【讨论】:

    【解决方案6】:

    我刚刚将 @Repository 注释添加到 Repository 接口和 @EnableJpaRepositories ("domain.repositroy-package") 到主类。效果很好。

    【讨论】:

    • 我不确定是否会帮助 OP,但如果有人使用 SpringBoot 并拥有 @EnableAutoConfiguration(exclude = {DataSource/HibernateJpa..etc} 这些排除可能会拒绝 spring 自动验证声明的注释在 CRUD 类中,因此删除排除可能会与在 Main 类中添加 @EnableJpaRepositories 一起修复它
    【解决方案7】:

    应用需要和扫描的包放在同一目录:

    【讨论】:

      【解决方案8】:

      我也面临同样的问题,因为我错过了使用实体注释标记我的 DAO 类。我在下面尝试并解决了错误。

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

      【讨论】:

        【解决方案9】:

        向 Spring Data JPA 存储库添加 @Repository 注释

        【讨论】:

          【解决方案10】:

          根据documentation你应该设置XML配置:

          <jpa:repositories base-package="com.kopylov.repository" />
          

          【讨论】:

          • 这是我的 appContext.xml 中的内容,但没有帮助
          【解决方案11】:

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

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

          附带说明,如果 setter 方法仅用于 @Autowired,则将 @Autowired/@Qualifier 直接放在您的成员上会更简洁。

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

          最后,您不需要 @Qualifier,因为只有一个类实现了 bean 定义,所以除非您有多个 ClientServiceClientRepository 的实现,否则您可以删除 @Qualifier

          【讨论】:

          • 你只是在写已经发布过的东西
          【解决方案12】:

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

          InvalidQueryException:键空间“mykeyspace”不存在

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

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

          【讨论】:

            【解决方案13】:

            查看Client表的表结构,如果db中的表结构与实体不匹配,你会得到这个错误..

            我遇到了这个错误,这是由于 db 表和实体之间主键的数据类型不匹配...

            【讨论】:

              【解决方案14】:

              如果在方法定义(“findBy”)中将某个字段描述为条件,则必须将该参数传递给方法,否则会出现“通过方法参数表示的不满足依赖关系”异常。

              public interface ClientRepository extends JpaRepository<Client, Integer> {
                     Client findByClientId();                ////WRONG !!!!
                     Client findByClientId(int clientId);    /// CORRECT 
              }
              

              *我假设您的 Client 实体具有 clientId 属性。

              【讨论】:

                【解决方案15】:

                那是他们包含生菜的版本不兼容。当我排除时,它对我有用。

                <!--Spring-Boot 2.0.0 -->
                    <dependency>
                                <groupId>org.springframework.boot</groupId>
                                <artifactId>spring-boot-starter-data-redis</artifactId>
                                <exclusions>
                                    <exclusion>
                                        <groupId>io.lettuce</groupId>
                                        <artifactId>lettuce-core</artifactId>
                                    </exclusion>
                                </exclusions>    
                            </dependency>
                            <dependency>
                                <groupId>redis.clients</groupId>
                                <artifactId>jedis</artifactId>
                            </dependency>
                

                【讨论】:

                  【解决方案16】:

                  另一个实例在您执行上述所有操作后仍会显示相同的错误。当您相应地更改代码时,提到的解决方案确保保留原件。因此,您可以轻松返回。因此,请再次检查 dispatcher-servelet 配置文件的基本包位置。运行应用程序时是否扫描所有相关包

                  <context:component-scan base-package="your.pakage.path.here"/>
                  

                  【讨论】:

                    【解决方案17】:

                    在组件定义上方添加@Component注解

                    【讨论】:

                      【解决方案18】:

                      如果派生查询方法存在语法错误,则可能会发生此错误。例如,如果实体类字段和派生方法的名称不匹配。

                      【讨论】:

                        【解决方案19】:

                        我遇到了这个问题,因为同一实体类中有重复的列名 staffIdstaff_id

                            private Integer staffId;
                            @ManyToOne(cascade = CascadeType.ALL, optional = false)
                            @JoinColumn(name = "staff_id")
                            private User user;
                        

                        【讨论】:

                          【解决方案20】:

                          只需在服务类顶部添加@Service 注解

                          【讨论】:

                            【解决方案21】:

                            查看您的 &lt;context:component-scan base-package="your package "/&gt; 是否在配置或上下文 xml 文件中丢失

                            【讨论】:

                              猜你喜欢
                              • 2021-06-20
                              • 2021-11-18
                              • 2020-07-24
                              • 2018-01-31
                              • 2021-05-26
                              • 2018-04-07
                              • 2020-11-16
                              • 2015-06-19
                              • 2019-06-15
                              相关资源
                              最近更新 更多