我想实现前面提到的 JPA 存储库 here。我想我的问题是我想使用 JPA 存储库本身的一些功能,我 @Autorwired
它但我不完全确定
错误信息:
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
到目前为止我尝试过的:
1- 尝试使用 setter-based @Autowired
而不是 field-based(但没有变化)
2- 尝试使用 @Lazy
也没有任何改变
**代码:**
CustomMajorRepository.java
public interface CustomMajorRepository {
void addStudentToMajor(Student student, Major major);
void increaseMajorLimit(UUID majorId, int amount);
}
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
@Lazy
public interface MajorRepository extends JpaRepository<Major, UUID>, CustomMajorRepository {
}
答案 0 :(得分:0)
我认为你应该通过构造函数或setter使用依赖注入,例如:
private MajorRepository majorRepository;
public CostumMajorRepositoryImpl(MajorRepository mh) {
this.majorRepository = mh;
}
这是关于 setter 和构造函数注入的 spring.io 文章: