DATACMNS-1255 - Extend configuration of CDI repository settings.
We now extended CDI repository configuration to allow configuration of EvaluationContextProvider, NamedQueries, QueryLookupStrategy keys and the repository base class. CdiRepositoryConfiguration is now an interface with default-methods only providing default configuration values. Original pull request: #272.
This commit is contained in:
committed by
Oliver Gierke
parent
036ccde5ce
commit
919090bf20
@@ -41,9 +41,9 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.data.repository.config.CustomRepositoryImplementationDetector;
|
||||
import org.springframework.data.repository.config.DefaultRepositoryConfiguration;
|
||||
import org.springframework.data.repository.config.RepositoryBeanNameGenerator;
|
||||
import org.springframework.data.repository.config.SpringDataAnnotationBeanNameGenerator;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -407,6 +407,34 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl
|
||||
+ "in order to use custom repository implementations");
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the configuration from {@link CdiRepositoryConfiguration} to {@link RepositoryFactorySupport} by looking up
|
||||
* the actual configuration.
|
||||
*
|
||||
* @param repositoryFactory will never be {@literal null}.
|
||||
* @since 2.1
|
||||
*/
|
||||
protected void applyConfiguration(RepositoryFactorySupport repositoryFactory) {
|
||||
applyConfiguration(repositoryFactory, lookupConfiguration(beanManager, qualifiers));
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the configuration from {@link CdiRepositoryConfiguration} to {@link RepositoryFactorySupport} by looking up
|
||||
* the actual configuration.
|
||||
*
|
||||
* @param repositoryFactory will never be {@literal null}.
|
||||
* @param configuration will never be {@literal null}.
|
||||
* @since 2.1
|
||||
*/
|
||||
protected static void applyConfiguration(RepositoryFactorySupport repositoryFactory,
|
||||
CdiRepositoryConfiguration configuration) {
|
||||
|
||||
configuration.getEvaluationContextProvider().ifPresent(repositoryFactory::setEvaluationContextProvider);
|
||||
configuration.getNamedQueries().ifPresent(repositoryFactory::setNamedQueries);
|
||||
configuration.getQueryLookupStrategy().ifPresent(repositoryFactory::setQueryLookupStrategyKey);
|
||||
configuration.getRepositoryBeanClass().ifPresent(repositoryFactory::setRepositoryBaseClass);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
@@ -417,17 +445,7 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl
|
||||
qualifiers.toString());
|
||||
}
|
||||
|
||||
static enum DefaultCdiRepositoryConfiguration implements CdiRepositoryConfiguration {
|
||||
|
||||
enum DefaultCdiRepositoryConfiguration implements CdiRepositoryConfiguration {
|
||||
INSTANCE;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.cdi.CdiRepositoryConfiguration#getRepositoryImplementationPostfix()
|
||||
*/
|
||||
@Override
|
||||
public String getRepositoryImplementationPostfix() {
|
||||
return DefaultRepositoryConfiguration.DEFAULT_REPOSITORY_IMPLEMENTATION_POSTFIX;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,10 @@
|
||||
|
||||
package org.springframework.data.repository.cdi;
|
||||
|
||||
import org.springframework.data.repository.config.DefaultRepositoryConfiguration;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.core.NamedQueries;
|
||||
import org.springframework.data.repository.query.EvaluationContextProvider;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
|
||||
/**
|
||||
@@ -27,20 +30,53 @@ import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
*/
|
||||
public interface CdiRepositoryConfiguration {
|
||||
|
||||
/**
|
||||
* Return the {@link EvaluationContextProvider} to use. Can be {@link Optional#empty()} .
|
||||
*
|
||||
* @return the optional {@link EvaluationContextProvider} base to use, can be {@link Optional#empty()}, must not be
|
||||
* {@literal null}.
|
||||
* @since 2.1
|
||||
*/
|
||||
default Optional<EvaluationContextProvider> getEvaluationContextProvider() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link NamedQueries} to use. Can be {@link Optional#empty()}.
|
||||
*
|
||||
* @return the optional named queries to use, can be {@link Optional#empty()}, must not be {@literal null}.
|
||||
* @since 2.1
|
||||
*/
|
||||
default Optional<NamedQueries> getNamedQueries() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link QueryLookupStrategy.Key} to lookup queries. Can be {@link Optional#empty()}.
|
||||
*
|
||||
* @return the lookup strategy to use, can be {@link Optional#empty()}, must not be {@literal null}.
|
||||
* @since 2.1
|
||||
*/
|
||||
default Optional<QueryLookupStrategy.Key> getQueryLookupStrategy() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link Class repository base class} to use. Can be {@link Optional#empty()} .
|
||||
*
|
||||
* @return the optional repository base to use, can be {@link Optional#empty()}, must not be {@literal null}.
|
||||
* @since 2.1
|
||||
*/
|
||||
default Optional<Class<?>> getRepositoryBeanClass() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the configured postfix to be used for looking up custom implementation classes.
|
||||
*
|
||||
* @return the postfix to use, must not be {@literal null}.
|
||||
*/
|
||||
String getRepositoryImplementationPostfix();
|
||||
|
||||
/**
|
||||
* Return the strategy to lookup queries.
|
||||
*
|
||||
* @return the lookup strategy to use.
|
||||
* @since 2.1
|
||||
*/
|
||||
default QueryLookupStrategy.Key getQueryLookupStrategy() {
|
||||
return DefaultRepositoryConfiguration.DEFAULT_QUERY_LOOKUP_STRATEGY;
|
||||
default String getRepositoryImplementationPostfix() {
|
||||
return "Impl";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,11 +39,18 @@ import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.config.CustomRepositoryImplementationDetector;
|
||||
import org.springframework.data.repository.core.NamedQueries;
|
||||
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.data.repository.query.DefaultEvaluationContextProvider;
|
||||
import org.springframework.data.repository.query.EvaluationContextProvider;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CdiRepositoryBean}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CdiRepositoryBeanUnitTests {
|
||||
@@ -55,6 +62,7 @@ public class CdiRepositoryBeanUnitTests {
|
||||
.singleton((Annotation) new CdiRepositoryExtensionSupport.DefaultAnnotationLiteral());
|
||||
|
||||
@Mock BeanManager beanManager;
|
||||
@Mock RepositoryFactorySupport repositoryFactory;
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void voidRejectsNullQualifiers() {
|
||||
@@ -154,6 +162,25 @@ public class CdiRepositoryBeanUnitTests {
|
||||
);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1255
|
||||
public void appliesRepositoryConfiguration() {
|
||||
|
||||
DummyCdiRepositoryBean<SampleRepository> bean = new DummyCdiRepositoryBean<SampleRepository>(NO_ANNOTATIONS,
|
||||
SampleRepository.class, beanManager) {
|
||||
@Override
|
||||
protected CdiRepositoryConfiguration lookupConfiguration(BeanManager beanManager, Set qualifiers) {
|
||||
return RepositoryConfiguration.INSTANCE;
|
||||
}
|
||||
};
|
||||
|
||||
bean.applyConfiguration(repositoryFactory);
|
||||
|
||||
verify(repositoryFactory).setEvaluationContextProvider(DefaultEvaluationContextProvider.INSTANCE);
|
||||
verify(repositoryFactory).setNamedQueries(PropertiesBasedNamedQueries.EMPTY);
|
||||
verify(repositoryFactory).setRepositoryBaseClass(String.class);
|
||||
verify(repositoryFactory).setQueryLookupStrategyKey(Key.CREATE);
|
||||
}
|
||||
|
||||
static class DummyCdiRepositoryBean<T> extends CdiRepositoryBean<T> {
|
||||
|
||||
DummyCdiRepositoryBean(Set<Annotation> qualifiers, Class<T> repositoryType, BeanManager beanManager) {
|
||||
@@ -168,12 +195,37 @@ public class CdiRepositoryBeanUnitTests {
|
||||
}
|
||||
|
||||
@Named("namedRepository")
|
||||
static interface SampleRepository extends Repository<Object, Serializable> {
|
||||
interface SampleRepository extends Repository<Object, Serializable> {
|
||||
|
||||
}
|
||||
|
||||
@StereotypeAnnotation
|
||||
static interface StereotypedSampleRepository {
|
||||
interface StereotypedSampleRepository {
|
||||
|
||||
}
|
||||
|
||||
enum RepositoryConfiguration implements CdiRepositoryConfiguration {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Optional<EvaluationContextProvider> getEvaluationContextProvider() {
|
||||
return Optional.of(DefaultEvaluationContextProvider.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<NamedQueries> getNamedQueries() {
|
||||
return Optional.of(PropertiesBasedNamedQueries.EMPTY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Key> getQueryLookupStrategy() {
|
||||
return Optional.of(Key.CREATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Class<?>> getRepositoryBeanClass() {
|
||||
return Optional.of(String.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user