[英]Unsatisfied dependency in spring
I want to implement JPA repositories as mentioned here .我想实现这里提到的 JPA 存储库。 I think my problem is I want to use some functions from the JPA Repository itself and I
@Autorwired
it But I'm not completely sure我想我的问题是我想使用 JPA 存储库本身的一些功能,我
@Autorwired
它但我不完全确定
Error Message:错误信息:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addBothStudentsHandler': Unsatisfied dependency expressed through method 'setMajorRepository' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'majorRepository' defined in io.asiam.tansiq.repositories.MajorRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot create inner bean '(inner bean)#78f146b5' of type [org.springframework.data.repository.core.support.RepositoryFragmentsFactoryBean] while setting bean property 'repositoryFragments'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#78f146b5': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customMajorRepositoryImplFragment': Cannot resolve reference to bean 'customMajorRepositoryImpl' while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customMajorRepositoryImpl': Unsatisfied dependency expressed through field 'majorRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'majorRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
What I have tried so far:到目前为止我尝试过的:
1- tried to use setter-based @Autowired
instead of field-based (But no change) 1- 尝试使用基于 setter 的
@Autowired
而不是基于字段的(但没有变化)
2- tried to use @Lazy
also nothing changed 2- 尝试使用
@Lazy
也没有任何改变
**Code: ** **代码: **
CustomMajorRepository.java CustomMajorRepository.java
public interface CustomMajorRepository {
void addStudentToMajor(Student student, Major major);
void increaseMajorLimit(UUID majorId, int amount);
}
CustomMajorRepositoryImpl.java CustomMajorRepositoryImpl.java
private MajorRepository majorRepository;
public MajorRepository getMajorRepository() {
return majorRepository;
}
@Autowired
public void setMajorRepository(MajorRepository majorRepository) {
this.majorRepository = majorRepository;
}
// rest of implementation details
MajorRepository.java主要存储库.java
@Lazy
public interface MajorRepository extends JpaRepository<Major, UUID>, CustomMajorRepository {
}
I think you should use dependency injection via constructor or setter, for example:我认为您应该通过构造函数或 setter 使用依赖注入,例如:
private MajorRepository majorRepository;
public CostumMajorRepositoryImpl(MajorRepository mh) {
this.majorRepository = mh;
}
Here is the spring.io article about the setter and constructor injection:这是关于 setter 和构造函数注入的 spring.io 文章:
https://spring.io/blog/2007/07/11/setter-injection-versus-constructor-injection-and-the-use-of-required/ https://spring.io/blog/2007/07/11/setter-injection-versus-constructor-injection-and-the-use-of-required/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.