gpt4 book ai didi

java - Spring JPA 数据 : Custom Generic Repositories and Services: UnsatisfiedDependencyException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:23 25 4
gpt4 key购买 nike

我正在为很多实体开发一个 Restful 服务。如果我们考虑两组父资源子资源,两个组成员在其组范围内具有相同的 CRUD 操作实现

因此,每一层不仅有一个泛型类。这是我的代码:

存储库:

具有所有实体使用的方法的基本存储库:

@Repository
public interface GenericRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
Page<T> findAll(Pageable pageable);
}

父资源存储库

@Repository
public interface EntityGenericRepository<T, ID extends Serializable> extends GenericRepository<T, ID> {
T findByName(String name);
}

子资源存储库

@Repository
public interface NestedEntityGenericRepository<T, ID extends Serializable> extends GenericRepository<T, ID> {
Page<T> findByFatherId(ID fatherId, Pageable pageable);
}

服务:

对于基地:

public interface GenericService<T,ID extends Serializable> {
Page<T> findAll(int page, int size);

T findById(ID id);
}

父亲:

public interface EntityGenericService<T, ID extends Serializable> extends GenericService<T, ID> {
T findByName(String name);

T save(T t);

void update(ID id, T t);

void softDelete(ID id);
}

对于 child :

public interface NestedEntityGenericService<T, ID extends Serializable> {
Page<T> findBySensorId(ID fatherId, int page, int size);

T save(ID fatherId, T t);

void update(ID fatherId, ID id, T t);

void softDelete(ID fatherId, ID id);
}

服务实现:

基础:

@Service
public class GenericServiceImpl<T,ID extends Serializable>
implements GenericService<T,ID> { //codes }

父亲:

@Service
public class EntityGenericServiceImpl<T, ID extends Serializable>
extends GenericServiceImpl<T, ID>
implements EntityGenericService<T, ID> {//codes}

对于 child :

@Service
public class NestedEntityGenericServiceImpl<T, U, ID extends Serializable>
extends EntityGenericServiceImpl<T, ID>
implements NestedEntityGenericService<T, ID> {//codes}

当我运行它时,它只会抛出 UnsatisfiedDependencyException。整个消息:

Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating
bean with name 'entityGenericServiceImpl': Unsatisfied dependency expressed through
field 'genericRepository': Error creating bean with name 'nestedEntityGenericRepository':
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException:
Not a managed type: class java.lang.Object; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'nestedEntityGenericRepository': Invocation of init method failed; nested exception is
java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object

搜索了很多,但没有找到解决这个问题的方法。感谢任何帮助

问候

最佳答案

我通过为每个实体创建具体的存储库来解决这个问题。我试图减少类(class)数量。所以我定义了 3 个通用存储库来完成定义为服务所有实体的所有其他存储库的工作。但我知道这是不可能的,必须在自定义存储库的最后一级定义具体的存储库才能与服务层交互。

原因在于反射(reflection)。 Spring 使用反射来完成幕后的所有工作,它必须知道它必须为哪个实体使用提供程序 (Hibernate)

需要注意的是,如果要创建通用的服务层,它的最后一层也必须有具体的实现。因为必须在某处声明具体的存储库

关于java - Spring JPA 数据 : Custom Generic Repositories and Services: UnsatisfiedDependencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51823461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com