繁体   English   中英

数据持久性的 Spring Boot 依赖注入错误

[英]Error With Spring Boot Dependency Injection For Data Persistence

我是 Spring Boot 的新手,但目前正在构建一个应用程序,它使用 Spring 不是为了它的 Web 应用程序功能,而是利用它的数据源功能(使用 JPA 注释)。 运行我的代码时,发生以下编译错误:


21-11-14 18:12:17.765 ERROR 13432 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'taskSerializableInteractor' defined in file [/Users/myname/softwarename/target/classes/api/TaskSerializableInteractor.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'taskSerializableRepository' defined in api.TaskSerializableRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class api.TaskSerializable

这个错误向我表明 Spring Boot 在处理应用程序的 API 类中定义的依赖注入时存在一些问题。 项目本身很大,我不知道这里调试需要哪些代码,但如果需要,我很乐意发布片段。 我已经用谷歌搜索了几个小时,不知道如何解决这个问题。 关于如何解决此错误的任何提示?

正如错误所说,您要注入的依赖项无法做到。 这可能发生在:

  1. 没有匹配的构造函数。 例如,如果你有这个类
//BAD PRACTICE
class Foo(){
    @Autowired
    private MyClass1 myClass1;
    @Autowired
    private MyClass2 myClass2;
    @Autowired
    private MyClass3 myClass3;

...
}

而且你没有定义任何构造函数,依赖没有被注入。 在这种情况下的正确方法:

class Foo(){
    private MyClass1 myClass1;
    private MyClass2 myClass2;
    private MyClass3 myClass3;

    @Autowired
    public Foo(MyClass1 myClass1, MyClass2 myClass2, MyClass3 myClass3){
        this.myClass1 = myClass1;
        this.myClass2 = myClass2;
        this.myClass3 = myClass3;
    }
}
  1. 您要注入的依赖项没有 @Component 并且没有定义 @Bean 来注入它。 您要注入的依赖项名称和在 @Bean 上定义的方法应该是相等的。 继续前面的例子:
@Configuration
public class MyBeanConfiguration(){

    @Bean
    public MyClass1 myClass1(){
        return new MyClass1(PARAMETERS)
    }

    ... SO ON ...
}

因此,检查classes/api/TaskSerializableInteractor是否具有正确的构造函数并且是否定义了 bean 的配置。

如果你能展示部分课程,最好有更正确的想法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM