Skip to content

Commit 919090b

Browse files
mp911deodrotbohm
authored andcommitted
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.
1 parent 036ccde commit 919090b

File tree

3 files changed

+128
-22
lines changed

3 files changed

+128
-22
lines changed

src/main/java/org/springframework/data/repository/cdi/CdiRepositoryBean.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
4242
import org.springframework.beans.factory.support.AbstractBeanDefinition;
4343
import org.springframework.data.repository.config.CustomRepositoryImplementationDetector;
44-
import org.springframework.data.repository.config.DefaultRepositoryConfiguration;
4544
import org.springframework.data.repository.config.RepositoryBeanNameGenerator;
4645
import org.springframework.data.repository.config.SpringDataAnnotationBeanNameGenerator;
46+
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
4747
import org.springframework.lang.Nullable;
4848
import org.springframework.util.Assert;
4949
import org.springframework.util.ClassUtils;
@@ -407,6 +407,34 @@ protected T create(CreationalContext<T> creationalContext, Class<T> repositoryTy
407407
+ "in order to use custom repository implementations");
408408
}
409409

410+
/**
411+
* Applies the configuration from {@link CdiRepositoryConfiguration} to {@link RepositoryFactorySupport} by looking up
412+
* the actual configuration.
413+
*
414+
* @param repositoryFactory will never be {@literal null}.
415+
* @since 2.1
416+
*/
417+
protected void applyConfiguration(RepositoryFactorySupport repositoryFactory) {
418+
applyConfiguration(repositoryFactory, lookupConfiguration(beanManager, qualifiers));
419+
}
420+
421+
/**
422+
* Applies the configuration from {@link CdiRepositoryConfiguration} to {@link RepositoryFactorySupport} by looking up
423+
* the actual configuration.
424+
*
425+
* @param repositoryFactory will never be {@literal null}.
426+
* @param configuration will never be {@literal null}.
427+
* @since 2.1
428+
*/
429+
protected static void applyConfiguration(RepositoryFactorySupport repositoryFactory,
430+
CdiRepositoryConfiguration configuration) {
431+
432+
configuration.getEvaluationContextProvider().ifPresent(repositoryFactory::setEvaluationContextProvider);
433+
configuration.getNamedQueries().ifPresent(repositoryFactory::setNamedQueries);
434+
configuration.getQueryLookupStrategy().ifPresent(repositoryFactory::setQueryLookupStrategyKey);
435+
configuration.getRepositoryBeanClass().ifPresent(repositoryFactory::setRepositoryBaseClass);
436+
}
437+
410438
/*
411439
* (non-Javadoc)
412440
* @see java.lang.Object#toString()
@@ -417,17 +445,7 @@ public String toString() {
417445
qualifiers.toString());
418446
}
419447

420-
static enum DefaultCdiRepositoryConfiguration implements CdiRepositoryConfiguration {
421-
448+
enum DefaultCdiRepositoryConfiguration implements CdiRepositoryConfiguration {
422449
INSTANCE;
423-
424-
/*
425-
* (non-Javadoc)
426-
* @see org.springframework.data.repository.cdi.CdiRepositoryConfiguration#getRepositoryImplementationPostfix()
427-
*/
428-
@Override
429-
public String getRepositoryImplementationPostfix() {
430-
return DefaultRepositoryConfiguration.DEFAULT_REPOSITORY_IMPLEMENTATION_POSTFIX;
431-
}
432450
}
433451
}

src/main/java/org/springframework/data/repository/cdi/CdiRepositoryConfiguration.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package org.springframework.data.repository.cdi;
1818

19-
import org.springframework.data.repository.config.DefaultRepositoryConfiguration;
19+
import java.util.Optional;
20+
21+
import org.springframework.data.repository.core.NamedQueries;
22+
import org.springframework.data.repository.query.EvaluationContextProvider;
2023
import org.springframework.data.repository.query.QueryLookupStrategy;
2124

2225
/**
@@ -28,19 +31,52 @@
2831
public interface CdiRepositoryConfiguration {
2932

3033
/**
31-
* Returns the configured postfix to be used for looking up custom implementation classes.
34+
* Return the {@link EvaluationContextProvider} to use. Can be {@link Optional#empty()} .
3235
*
33-
* @return the postfix to use, must not be {@literal null}.
36+
* @return the optional {@link EvaluationContextProvider} base to use, can be {@link Optional#empty()}, must not be
37+
* {@literal null}.
38+
* @since 2.1
39+
*/
40+
default Optional<EvaluationContextProvider> getEvaluationContextProvider() {
41+
return Optional.empty();
42+
}
43+
44+
/**
45+
* Return the {@link NamedQueries} to use. Can be {@link Optional#empty()}.
46+
*
47+
* @return the optional named queries to use, can be {@link Optional#empty()}, must not be {@literal null}.
48+
* @since 2.1
49+
*/
50+
default Optional<NamedQueries> getNamedQueries() {
51+
return Optional.empty();
52+
}
53+
54+
/**
55+
* Return the {@link QueryLookupStrategy.Key} to lookup queries. Can be {@link Optional#empty()}.
56+
*
57+
* @return the lookup strategy to use, can be {@link Optional#empty()}, must not be {@literal null}.
58+
* @since 2.1
3459
*/
35-
String getRepositoryImplementationPostfix();
60+
default Optional<QueryLookupStrategy.Key> getQueryLookupStrategy() {
61+
return Optional.empty();
62+
}
3663

3764
/**
38-
* Return the strategy to lookup queries.
65+
* Return the {@link Class repository base class} to use. Can be {@link Optional#empty()} .
3966
*
40-
* @return the lookup strategy to use.
67+
* @return the optional repository base to use, can be {@link Optional#empty()}, must not be {@literal null}.
4168
* @since 2.1
4269
*/
43-
default QueryLookupStrategy.Key getQueryLookupStrategy() {
44-
return DefaultRepositoryConfiguration.DEFAULT_QUERY_LOOKUP_STRATEGY;
70+
default Optional<Class<?>> getRepositoryBeanClass() {
71+
return Optional.empty();
72+
}
73+
74+
/**
75+
* Returns the configured postfix to be used for looking up custom implementation classes.
76+
*
77+
* @return the postfix to use, must not be {@literal null}.
78+
*/
79+
default String getRepositoryImplementationPostfix() {
80+
return "Impl";
4581
}
4682
}

src/test/java/org/springframework/data/repository/cdi/CdiRepositoryBeanUnitTests.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,18 @@
3939
import org.mockito.junit.MockitoJUnitRunner;
4040
import org.springframework.data.repository.Repository;
4141
import org.springframework.data.repository.config.CustomRepositoryImplementationDetector;
42+
import org.springframework.data.repository.core.NamedQueries;
43+
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
44+
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
45+
import org.springframework.data.repository.query.DefaultEvaluationContextProvider;
46+
import org.springframework.data.repository.query.EvaluationContextProvider;
47+
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
4248

4349
/**
4450
* Unit tests for {@link CdiRepositoryBean}.
4551
*
4652
* @author Oliver Gierke
53+
* @author Mark Paluch
4754
*/
4855
@RunWith(MockitoJUnitRunner.class)
4956
public class CdiRepositoryBeanUnitTests {
@@ -55,6 +62,7 @@ public class CdiRepositoryBeanUnitTests {
5562
.singleton((Annotation) new CdiRepositoryExtensionSupport.DefaultAnnotationLiteral());
5663

5764
@Mock BeanManager beanManager;
65+
@Mock RepositoryFactorySupport repositoryFactory;
5866

5967
@Test(expected = IllegalArgumentException.class)
6068
public void voidRejectsNullQualifiers() {
@@ -154,6 +162,25 @@ protected SampleRepository create( //
154162
);
155163
}
156164

165+
@Test // DATACMNS-1255
166+
public void appliesRepositoryConfiguration() {
167+
168+
DummyCdiRepositoryBean<SampleRepository> bean = new DummyCdiRepositoryBean<SampleRepository>(NO_ANNOTATIONS,
169+
SampleRepository.class, beanManager) {
170+
@Override
171+
protected CdiRepositoryConfiguration lookupConfiguration(BeanManager beanManager, Set qualifiers) {
172+
return RepositoryConfiguration.INSTANCE;
173+
}
174+
};
175+
176+
bean.applyConfiguration(repositoryFactory);
177+
178+
verify(repositoryFactory).setEvaluationContextProvider(DefaultEvaluationContextProvider.INSTANCE);
179+
verify(repositoryFactory).setNamedQueries(PropertiesBasedNamedQueries.EMPTY);
180+
verify(repositoryFactory).setRepositoryBaseClass(String.class);
181+
verify(repositoryFactory).setQueryLookupStrategyKey(Key.CREATE);
182+
}
183+
157184
static class DummyCdiRepositoryBean<T> extends CdiRepositoryBean<T> {
158185

159186
DummyCdiRepositoryBean(Set<Annotation> qualifiers, Class<T> repositoryType, BeanManager beanManager) {
@@ -168,12 +195,37 @@ protected T create(CreationalContext<T> creationalContext, Class<T> repositoryTy
168195
}
169196

170197
@Named("namedRepository")
171-
static interface SampleRepository extends Repository<Object, Serializable> {
198+
interface SampleRepository extends Repository<Object, Serializable> {
172199

173200
}
174201

175202
@StereotypeAnnotation
176-
static interface StereotypedSampleRepository {
203+
interface StereotypedSampleRepository {
204+
205+
}
206+
207+
enum RepositoryConfiguration implements CdiRepositoryConfiguration {
208+
209+
INSTANCE;
177210

211+
@Override
212+
public Optional<EvaluationContextProvider> getEvaluationContextProvider() {
213+
return Optional.of(DefaultEvaluationContextProvider.INSTANCE);
214+
}
215+
216+
@Override
217+
public Optional<NamedQueries> getNamedQueries() {
218+
return Optional.of(PropertiesBasedNamedQueries.EMPTY);
219+
}
220+
221+
@Override
222+
public Optional<Key> getQueryLookupStrategy() {
223+
return Optional.of(Key.CREATE);
224+
}
225+
226+
@Override
227+
public Optional<Class<?>> getRepositoryBeanClass() {
228+
return Optional.of(String.class);
229+
}
178230
}
179231
}

0 commit comments

Comments
 (0)