我正在尝试为项目中的MongoRepositories子集提供一些自定义通用方法。
我有以下基本存储库:
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends MongoRepository<T, ID> {
RangeResponse<T> findAllInRange(RangeRequest rangeRequest);
}
因此实现:
public abstract class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleMongoRepository<T, ID>
implements BaseRepository<T, ID> {
private MongoOperations mongoOperations;
private MongoEntityInformation<T, ID> entityInformation;
public BaseRepositoryImpl(final MongoEntityInformation<T, ID> entityInformation, final MongoOperations mongoOperations) {
super(entityInformation, mongoOperations);
this.entityInformation = entityInformation;
this.mongoOperations = mongoOperations;
}
private String getCollectionName() {
return entityInformation.getCollectionName();
}
private Class<T> getJavaType() {
return entityInformation.getJavaType();
}
@Override
public RangeResponse<T> findAllInRange(final RangeRequest rangeRequest) {
long count = count();
final Query query = new Query()
.skip(rangeRequest.getOffset())
.limit(rangeRequest.getLimit() - rangeRequest.getOffset() + 1)
.with(rangeRequest.getSort());
List<T> clients = this.mongoOperations.find(query, getJavaType(), getCollectionName());
return new RangeResponse<>(clients, rangeRequest, count);
}
}
但是,如果我尝试将其添加到现有存储库中
@Repository
public interface MyRepository extends MongoRepository<MyEntity, String>, BaseRepository<MyEntity, String> {
}
我收到以下异常:
[...]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findAllInRange found for type MyEntity !
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
[...]
我可以使用'repositoryBaseClass'使它工作:
@Configuration
@EnableMongoAuditing
@EnableMongoRepositories(
basePackages = {
"com.example.repository",
}
repositoryBaseClass = BaseRepositoryImpl.class
)
public class MongoConfig {
}
但是我不希望我的所有存储库都继承此自定义方法。
有什么想法吗?