Skip to content

Commit d70508e

Browse files
Add support for using custom BeanNameGenerator.
Closes: #4756
1 parent 1dccb98 commit d70508e

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/config/EnableMongoRepositories.java

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.annotation.Target;
2424

2525
import org.springframework.beans.factory.FactoryBean;
26+
import org.springframework.beans.factory.support.BeanNameGenerator;
2627
import org.springframework.context.annotation.ComponentScan.Filter;
2728
import org.springframework.context.annotation.Import;
2829
import org.springframework.data.mongodb.core.MongoTemplate;
@@ -117,6 +118,13 @@
117118
*/
118119
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
119120

121+
/**
122+
* Configure a specific {@link BeanNameGenerator} to be used when creating the repository beans.
123+
* @return the {@link BeanNameGenerator} to be used or the base {@link BeanNameGenerator} interface to indicate context default.
124+
* @since 4.4
125+
*/
126+
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
127+
120128
/**
121129
* Configures the name of the {@link MongoTemplate} bean to be used with the repositories detected.
122130
*

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/config/EnableReactiveMongoRepositories.java

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.lang.annotation.Target;
2525

2626
import org.springframework.beans.factory.FactoryBean;
27+
import org.springframework.beans.factory.support.BeanNameGenerator;
2728
import org.springframework.context.annotation.ComponentScan.Filter;
2829
import org.springframework.context.annotation.Import;
2930
import org.springframework.data.mongodb.core.MongoTemplate;
@@ -119,6 +120,13 @@
119120
*/
120121
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
121122

123+
/**
124+
* Configure a specific {@link BeanNameGenerator} to be used when creating the repository beans.
125+
* @return the {@link BeanNameGenerator} to be used or the base {@link BeanNameGenerator} interface to indicate context default.
126+
* @since 4.4
127+
*/
128+
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
129+
122130
/**
123131
* Configures the name of the {@link MongoTemplate} bean to be used with the repositories detected.
124132
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.repository.config;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.util.Arrays;
21+
import java.util.stream.Stream;
22+
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.Arguments;
26+
import org.junit.jupiter.params.provider.MethodSource;
27+
import org.springframework.beans.factory.config.BeanDefinition;
28+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
29+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
30+
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
31+
import org.springframework.core.env.StandardEnvironment;
32+
import org.springframework.core.io.DefaultResourceLoader;
33+
import org.springframework.core.type.AnnotationMetadata;
34+
import org.springframework.data.mongodb.repository.PersonRepository;
35+
36+
/**
37+
* @author Christoph Strobl
38+
*/
39+
class MongoRepositoriesRegistrarUnitTests {
40+
41+
private BeanDefinitionRegistry registry;
42+
43+
@BeforeEach
44+
void setUp() {
45+
registry = new DefaultListableBeanFactory();
46+
}
47+
48+
@ParameterizedTest // GH-499, GH-3440
49+
@MethodSource(value = { "args" })
50+
void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) {
51+
52+
MongoRepositoriesRegistrar registrar = new MongoRepositoriesRegistrar();
53+
registrar.setResourceLoader(new DefaultResourceLoader());
54+
registrar.setEnvironment(new StandardEnvironment());
55+
registrar.registerBeanDefinitions(metadata, registry);
56+
57+
Iterable<String> names = Arrays.asList(registry.getBeanDefinitionNames());
58+
assertThat(names).contains(beanNames);
59+
}
60+
61+
static Stream<Arguments> args() {
62+
return Stream.of(
63+
Arguments.of(AnnotationMetadata.introspect(Config.class),
64+
new String[] { "personRepository", "samplePersonRepository", "contactRepository" }),
65+
Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class),
66+
new String[] { "personREPO", "samplePersonREPO", "contactREPO" }));
67+
}
68+
69+
@EnableMongoRepositories(basePackageClasses = PersonRepository.class)
70+
private class Config {
71+
72+
}
73+
74+
@EnableMongoRepositories(basePackageClasses = PersonRepository.class, nameGenerator = MyBeanNameGenerator.class)
75+
private class ConfigWithBeanNameGenerator {
76+
77+
}
78+
79+
static class MyBeanNameGenerator extends AnnotationBeanNameGenerator {
80+
81+
@Override
82+
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
83+
return super.generateBeanName(definition, registry).replaceAll("Repository", "REPO");
84+
}
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.repository.config;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.util.Arrays;
21+
import java.util.stream.Stream;
22+
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.Arguments;
26+
import org.junit.jupiter.params.provider.MethodSource;
27+
import org.springframework.beans.factory.config.BeanDefinition;
28+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
29+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
30+
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
31+
import org.springframework.core.env.StandardEnvironment;
32+
import org.springframework.core.io.DefaultResourceLoader;
33+
import org.springframework.core.type.AnnotationMetadata;
34+
import org.springframework.data.mongodb.repository.PersonRepository;
35+
36+
/**
37+
* @author Christoph Strobl
38+
*/
39+
class ReactiveMongoRepositoriesRegistrarUnitTests {
40+
41+
private BeanDefinitionRegistry registry;
42+
43+
@BeforeEach
44+
void setUp() {
45+
registry = new DefaultListableBeanFactory();
46+
}
47+
48+
@ParameterizedTest // GH-499, GH-3440
49+
@MethodSource(value = { "args" })
50+
void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) {
51+
52+
ReactiveMongoRepositoriesRegistrar registrar = new ReactiveMongoRepositoriesRegistrar();
53+
registrar.setResourceLoader(new DefaultResourceLoader());
54+
registrar.setEnvironment(new StandardEnvironment());
55+
registrar.registerBeanDefinitions(metadata, registry);
56+
57+
Iterable<String> names = Arrays.asList(registry.getBeanDefinitionNames());
58+
assertThat(names).contains(beanNames);
59+
}
60+
61+
static Stream<Arguments> args() {
62+
return Stream.of(
63+
Arguments.of(AnnotationMetadata.introspect(Config.class), new String[] { "reactivePersonRepository" }),
64+
Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class),
65+
new String[] { "reactivePersonREPO" }));
66+
}
67+
68+
@EnableReactiveMongoRepositories(basePackageClasses = PersonRepository.class)
69+
private class Config {
70+
71+
}
72+
73+
@EnableReactiveMongoRepositories(basePackageClasses = PersonRepository.class,
74+
nameGenerator = MyBeanNameGenerator.class)
75+
private class ConfigWithBeanNameGenerator {
76+
77+
}
78+
79+
static class MyBeanNameGenerator extends AnnotationBeanNameGenerator {
80+
81+
@Override
82+
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
83+
return super.generateBeanName(definition, registry).replaceAll("Repository", "REPO");
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)