Skip to content

Commit ec3ba10

Browse files
Polishing.
Delegate annotation attribute resolution to configured BeanNameGenerator resolving method and test for enabling reactive repositories. Original Pull Request: #3083
1 parent 2a8402c commit ec3ba10

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* @author Mark Paluch
5454
* @author Johannes Englmeier
5555
* @author Florian Cramer
56+
* @author Christoph Strobl
5657
*/
5758
public class AnnotationRepositoryConfigurationSource extends RepositoryConfigurationSourceSupport {
5859

@@ -314,15 +315,10 @@ private static BeanNameGenerator configuredOrDefaultBeanNameGenerator(Annotation
314315
Class<? extends Annotation> annotation, ClassLoader beanClassLoader,
315316
@Nullable BeanNameGenerator importBeanNameGenerator) {
316317

317-
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(annotation.getName());
318-
319-
if (annotationAttributes != null) {
320-
321-
BeanNameGenerator beanNameGenerator = getBeanNameGenerator(annotationAttributes, beanClassLoader);
318+
BeanNameGenerator beanNameGenerator = getConfiguredBeanNameGenerator(metadata, annotation, beanClassLoader);
322319

323-
if (beanNameGenerator != null) {
324-
return beanNameGenerator;
325-
}
320+
if (beanNameGenerator != null) {
321+
return beanNameGenerator;
326322
}
327323

328324
return defaultBeanNameGenerator(importBeanNameGenerator);
@@ -348,26 +344,30 @@ private static BeanNameGenerator defaultBeanNameGenerator(@Nullable BeanNameGene
348344
}
349345

350346
/**
351-
* Obtain a configured {@link BeanNameGenerator}.
347+
* Obtain a configured {@link BeanNameGenerator} if present.
352348
*
353349
* @param beanClassLoader a class loader to load the configured {@link BeanNameGenerator} class in case it was
354350
* configured as String instead of a Class instance.
355-
* @return the bean name generator.
351+
* @return the bean name generator or {@literal null} if not configured.
356352
*/
357353
@Nullable
358354
@SuppressWarnings("unchecked")
359-
private static BeanNameGenerator getBeanNameGenerator(Map<String, Object> annotationAttributes,
360-
ClassLoader beanClassLoader) {
355+
private static BeanNameGenerator getConfiguredBeanNameGenerator(AnnotationMetadata metadata,
356+
Class<? extends Annotation> annotation, ClassLoader beanClassLoader) {
361357

362-
Object configuredBeanNameGenerator = annotationAttributes.get(BEAN_NAME_GENERATOR);
358+
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(annotation.getName());
359+
if(annotationAttributes == null || !annotationAttributes.containsKey(BEAN_NAME_GENERATOR)) {
360+
return null;
361+
}
363362

363+
Object configuredBeanNameGenerator = annotationAttributes.get(BEAN_NAME_GENERATOR);
364364
if (configuredBeanNameGenerator == null) {
365365
return null;
366366
}
367367

368-
if (configuredBeanNameGenerator instanceof String cbng) {
368+
if (configuredBeanNameGenerator instanceof String beanNameGeneratorTypeName) {
369369
try {
370-
configuredBeanNameGenerator = ClassUtils.forName(cbng, beanClassLoader);
370+
configuredBeanNameGenerator = ClassUtils.forName(beanNameGeneratorTypeName, beanClassLoader);
371371
} catch (Exception o_O) {
372372
throw new RuntimeException(o_O);
373373
}

src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
*/
1616
package org.springframework.data.repository.config;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
import static org.mockito.Mockito.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
20+
import static org.mockito.Mockito.mock;
2021

2122
import java.lang.annotation.Retention;
2223
import java.lang.annotation.RetentionPolicy;
@@ -36,14 +37,17 @@
3637
import org.springframework.core.type.StandardAnnotationMetadata;
3738
import org.springframework.data.repository.PagingAndSortingRepository;
3839
import org.springframework.data.repository.config.basepackage.repo.PersonRepository;
40+
import org.springframework.data.repository.core.support.DummyReactiveRepositoryFactory;
3941
import org.springframework.data.repository.core.support.DummyRepositoryFactory;
42+
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
4043

4144
/**
4245
* Unit tests for {@link AnnotationRepositoryConfigurationSource}.
4346
*
4447
* @author Oliver Gierke
4548
* @author Thomas Darimont
4649
* @author Mark Paluch
50+
* @author Christoph Strobl
4751
*/
4852
class AnnotationRepositoryConfigurationSourceUnitTests {
4953

@@ -180,6 +184,17 @@ void considerBeanNameGenerator() {
180184
assertThat(getConfigSource(DefaultConfiguration.class).generateBeanName(bd)).isEqualTo("personRepository");
181185
}
182186

187+
@Test // GH-3082
188+
void considerBeanNameGeneratorForReactiveRepos() {
189+
190+
RootBeanDefinition bd = new RootBeanDefinition(DummyReactiveRepositoryFactory.class);
191+
bd.getConstructorArgumentValues().addGenericArgumentValue(ReactivePersonRepository.class);
192+
193+
assertThat(getConfigSource(ConfigurationWithBeanNameGenerator.class).generateBeanName(bd))
194+
.isEqualTo(ReactivePersonRepository.class.getName());
195+
assertThat(getConfigSource(DefaultConfiguration.class).generateBeanName(bd)).isEqualTo("annotationRepositoryConfigurationSourceUnitTests.ReactivePersonRepository");
196+
}
197+
183198
private AnnotationRepositoryConfigurationSource getConfigSource(Class<?> type) {
184199

185200
AnnotationMetadata metadata = new StandardAnnotationMetadata(type, true);
@@ -204,6 +219,9 @@ static class ConfigurationWithExplicitFilter {}
204219
@EnableRepositories(nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)
205220
static class ConfigurationWithBeanNameGenerator {}
206221

222+
@EnableReactiveRepositories(nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)
223+
static class ReactiveConfigurationWithBeanNameGenerator {}
224+
207225
@Retention(RetentionPolicy.RUNTIME)
208226
@interface SampleAnnotation {
209227

@@ -214,4 +232,6 @@ static class ConfigurationWithBeanNameGenerator {}
214232

215233
@SampleAnnotation
216234
static class ConfigWithSampleAnnotation {}
235+
236+
interface ReactivePersonRepository extends ReactiveCrudRepository<Person, String> {}
217237
}

src/test/java/org/springframework/data/repository/config/EnableReactiveRepositories.java

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.annotation.Retention;
2020
import java.lang.annotation.RetentionPolicy;
2121

22+
import org.springframework.beans.factory.support.BeanNameGenerator;
2223
import org.springframework.context.annotation.ComponentScan.Filter;
2324
import org.springframework.context.annotation.Import;
2425
import org.springframework.data.repository.core.support.ReactiveDummyRepositoryFactoryBean;
@@ -43,6 +44,8 @@
4344

4445
Class<?> repositoryBaseClass() default ReactiveSortingRepository.class;
4546

47+
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
48+
4649
String namedQueriesLocation() default "";
4750

4851
String repositoryImplementationPostfix() default "Impl";

src/test/java/org/springframework/data/repository/config/RepositoryBeanDefinitionRegistrarSupportUnitTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ void registersBeanDefinitionForFoundBean() {
7777
assertNoBeanDefinitionRegisteredFor("profileRepository");
7878
}
7979

80-
8180
@Test // GH-2584
8281
void shouldExposeFragmentsAsBean() {
8382

@@ -202,7 +201,8 @@ static class LimitsImplementationBasePackages {}
202201
@EnableRepositories(basePackageClasses = MyNestedRepository.class, considerNestedRepositories = true,
203202
excludeFilters = {
204203
@Filter(type = FilterType.ASSIGNABLE_TYPE,
205-
value = RepositoryConfigurationExtensionSupportUnitTests.ReactiveRepository.class),
204+
value = { RepositoryConfigurationExtensionSupportUnitTests.ReactiveRepository.class,
205+
AnnotationRepositoryConfigurationSourceUnitTests.ReactivePersonRepository.class }),
206206
@Filter(type = FilterType.ASSIGNABLE_TYPE, value = MyOtherRepository.class) })
207207
static class NestedRepositoriesConfiguration {}
208208

0 commit comments

Comments
 (0)