Spring JPA Data:自定义通用仓库和服务:UnsatisfiedDependencyException

3

我正在开发一个具有许多实体的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);
}

同时适用于子元素:

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}

而对于儿童:

@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

我搜索了很多但都没有找到解决此问题的方法。非常感谢您的帮助。

谢谢!


@Cepr0 谢谢你的回复。但是如果我这样做,就无法满足泛型的目的,因为我仍然会有重复的方法来完成同样的工作,当然,我也不能使用在存储库中定义的自定义方法。 - HMD
1
你如何创建自己的通用存储库的具体实现? - Simon Martinelli
你能展示一下代码吗? - Simon Martinelli
1
@SimonMartinelli 我已经修复了。使用Spring Repositories时,接口必须在最后一级中定义为具体的。 - HMD
1
好的。你能把这个作为答案发布吗?谢谢。 - Simon Martinelli
显示剩余2条评论
1个回答

3
我通过为每个实体创建具体的存储库来解决了这个问题。我试图减少类的数量,因此我定义了3个通用存储库来执行为所有实体定义的所有其他存储库的工作。但是我明白了这是不可能的,必须在自定义存储库的最后一级中定义具体的存储库以与服务层交互。
原因是反射。Spring使用反射来完成幕后所有操作,它必须知道要使用提供程序(Hibernate)的哪个实体。
需要注意的是,如果您想创建通用服务层,则其最后一级必须具有具体实现,因为必须在某个地方声明具体的存储库。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接