java—spring中未满足的依赖关系

dsf9zpds  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(452)

我想实现这里提到的jpa存储库。我想我的问题是我想使用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的 @Autowired 而不是基于字段(但无更改)
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 {
}
bsxbgnwa

bsxbgnwa1#

对我来说,它是这样工作的:

@Repository
public class CustomMajorRepositoryImpl implements CustomMajorRepository {

    private MajorRepository majorRepository;

    @Autowired
    public void setReservationRepository(@Lazy ReservationRepository reservationRepository) {
        this.reservationRepository = reservationRepository;
    }

    // other methods
}
kse8i1jr

kse8i1jr2#

我认为您应该通过构造函数或setter使用依赖项注入,例如:

private MajorRepository majorRepository;

    public CostumMajorRepositoryImpl(MajorRepository mh) {
        this.majorRepository = mh;
    }

下面是关于setter和构造函数注入的spring.io文章:
https://spring.io/blog/2007/07/11/setter-injection-versus-constructor-injection-and-the-use-of-required/

相关问题