Я пытаюсь сделать несколько пользовательских универсальных методов доступными для подмножества 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 {
}
Но я не хочу, чтобы все мои репозиторииунаследуйте этот пользовательский метод.
Есть идеи?