我正在开发一个具有许多实体的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
我搜索了很多但都没有找到解决此问题的方法。非常感谢您的帮助。
谢谢!