DATAJDBC-572 - Enable specification of a repository base class.

Added the field to `EnableJdbcRepository` and respect it's value by instantiating the repository base class reflectively.

Original pull request: #235.
This commit is contained in:
Jens Schauder
2020-06-29 13:23:29 +02:00
committed by Mark Paluch
parent b28e4e07c6
commit d3f4bce539
3 changed files with 74 additions and 7 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Import;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
/**
* Annotation to enable JDBC repositories. Will scan the package of the annotated configuration class for Spring Data
@@ -112,4 +113,13 @@ public @interface EnableJdbcRepositories {
* be used to create repositories discovered through this annotation. Defaults to {@code defaultDataAccessStrategy}.
*/
String dataAccessStrategyRef() default "";
/**
* Configure the repository base class to be used to create repository proxies for this particular configuration.
*
* @return
* @since 2.1
*/
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.jdbc.repository.support;
import java.io.Serializable;
import java.util.Optional;
import org.springframework.context.ApplicationEventPublisher;
@@ -26,6 +27,7 @@ import org.springframework.data.mapping.callback.EntityCallbacks;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.repository.query.RelationalEntityInformation;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
@@ -115,14 +117,13 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
JdbcAggregateTemplate template = new JdbcAggregateTemplate(publisher, context, converter, accessStrategy);
SimpleJdbcRepository<?, Object> repository = new SimpleJdbcRepository<>(template,
context.getRequiredPersistentEntity(repositoryInformation.getDomainType()));
if (entityCallbacks != null) {
template.setEntityCallbacks(entityCallbacks);
}
return repository;
RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(repositoryInformation.getDomainType());
return getTargetRepositoryViaReflection(repositoryInformation.getRepositoryBaseClass(), template, persistentEntity);
}
/*

View File

@@ -21,6 +21,7 @@ import static org.mockito.Mockito.*;
import lombok.Data;
import java.lang.reflect.Field;
import java.util.Optional;
import javax.sql.DataSource;
@@ -33,6 +34,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
@@ -40,6 +42,7 @@ import org.springframework.data.jdbc.core.convert.SqlGeneratorSource;
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositoriesIntegrationTests.TestConfiguration;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.repository.CrudRepository;
@@ -90,9 +93,10 @@ public class EnableJdbcRepositoriesIntegrationTests {
assertThat(repository).isNotNull();
Iterable<DummyEntity> all = repository.findAll();
long count = repository.count();
assertThat(all).isNotNull();
// the custom base class has a result of 23 hard wired.
assertThat(count).isEqualTo(23L);
}
@Test // DATAJDBC-166
@@ -128,7 +132,8 @@ public class EnableJdbcRepositoriesIntegrationTests {
@ComponentScan("org.springframework.data.jdbc.testing")
@EnableJdbcRepositories(considerNestedRepositories = true,
includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = DummyRepository.class),
jdbcOperationsRef = "qualifierJdbcOperations", dataAccessStrategyRef = "qualifierDataAccessStrategy")
jdbcOperationsRef = "qualifierJdbcOperations", dataAccessStrategyRef = "qualifierDataAccessStrategy",
repositoryBaseClass = DummyRepositoryBaseClass.class)
static class TestConfiguration {
@Bean
@@ -162,4 +167,55 @@ public class EnableJdbcRepositoriesIntegrationTests {
return DialectResolver.getDialect(operations.getJdbcOperations());
}
}
private static class DummyRepositoryBaseClass{
DummyRepositoryBaseClass(JdbcAggregateTemplate template, PersistentEntity<?,?> persistentEntity) {
}
public Object save(Object o) {
return null;
}
public Iterable saveAll(Iterable iterable) {
return null;
}
public Optional findById(Object o) {
return Optional.empty();
}
public boolean existsById(Object o) {
return false;
}
public Iterable findAll() {
return null;
}
public Iterable findAllById(Iterable iterable) {
return null;
}
public long count() {
return 23L;
}
public void deleteById(Object o) {
}
public void delete(Object o) {
}
public void deleteAll(Iterable iterable) {
}
public void deleteAll() {
}
}
}