简体   繁体   English

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

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

I am new to Spring Boot, but am currently building an application which used Spring not for its web app capabilities, but rather takes advantage of its data source capabilities (using JPA annotations).我是 Spring Boot 的新手,但目前正在构建一个应用程序,它使用 Spring 不是为了它的 Web 应用程序功能,而是利用它的数据源功能(使用 JPA 注释)。 When running my code, the following compilation error takes place:运行我的代码时,发生以下编译错误:


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

This error suggests to me that Spring Boot is having some issues with handling the dependency injection defined in the application's API classes.这个错误向我表明 Spring Boot 在处理应用程序的 API 类中定义的依赖注入时存在一些问题。 The project itself is quite large and I don't know which code is required for debugging here, but I'm happy to post snippets if required.项目本身很大,我不知道这里调试需要哪些代码,但如果需要,我很乐意发布片段。 I've Googled for hours and have no clue how to figure this out.我已经用谷歌搜索了几个小时,不知道如何解决这个问题。 Any tips on how I can go about resolving this error?关于如何解决此错误的任何提示?

As the error says, the dependency you want to inject couldn't do it.正如错误所说,您要注入的依赖项无法做到。 This may happen when:这可能发生在:

  1. There is no matching constructor.没有匹配的构造函数。 For example, if you have this class例如,如果你有这个类
//BAD PRACTICE
class Foo(){
    @Autowired
    private MyClass1 myClass1;
    @Autowired
    private MyClass2 myClass2;
    @Autowired
    private MyClass3 myClass3;

...
}

And you don't define any constructor, the dependency not being inject.而且你没有定义任何构造函数,依赖没有被注入。 The correct way in this case:在这种情况下的正确方法:

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. The dependency you want to inject is no @Component and there is no @Bean defined to inject it.您要注入的依赖项没有 @Component 并且没有定义 @Bean 来注入它。 The name of dependency you want to inject and method defined on @Bean should be equals.您要注入的依赖项名称和在 @Bean 上定义的方法应该是相等的。 Continue with previous example:继续前面的例子:
@Configuration
public class MyBeanConfiguration(){

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

    ... SO ON ...
}

So that, check if classes/api/TaskSerializableInteractor have correct constructor and there is a configuration of beans defined.因此,检查classes/api/TaskSerializableInteractor是否具有正确的构造函数并且是否定义了 bean 的配置。

If you could shown part of the class, it's better to have more correct idea.如果你能展示部分课程,最好有更正确的想法。

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

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