Skip to content

Commit 9842b1c

Browse files
Add support for using custom BeanNameGenerator.
Closes: #496
1 parent e6955d6 commit 9842b1c

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

src/main/java/org/springframework/data/ldap/repository/config/EnableLdapRepositories.java

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
2424

25+
import org.springframework.beans.factory.support.BeanNameGenerator;
2526
import org.springframework.context.annotation.ComponentScan.Filter;
2627
import org.springframework.context.annotation.Import;
2728
import org.springframework.data.ldap.repository.support.LdapRepositoryFactoryBean;
@@ -34,6 +35,7 @@
3435
*
3536
* @author Mattias Hellborg Arthursson
3637
* @author Mark Paluch
38+
* @author Christoph Strobl
3739
*/
3840
@Target(ElementType.TYPE)
3941
@Retention(RetentionPolicy.RUNTIME)
@@ -114,6 +116,13 @@
114116
*/
115117
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
116118

119+
/**
120+
* Configure a specific {@link BeanNameGenerator} to be used when creating the repository beans.
121+
* @return the {@link BeanNameGenerator} to be used or the base {@link BeanNameGenerator} interface to indicate context default.
122+
* @since 3.4
123+
*/
124+
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
125+
117126
/**
118127
* Configures the name of the {@link org.springframework.ldap.core.LdapTemplate} bean to be used with the repositories
119128
* detected.
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.ldap.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.ldap.config.DummyLdapRepository;
35+
36+
/**
37+
* @author Christoph Strobl
38+
*/
39+
class LdapRepositoriesRegistrarUnitTests {
40+
41+
private BeanDefinitionRegistry registry;
42+
43+
@BeforeEach
44+
void setUp() {
45+
registry = new DefaultListableBeanFactory();
46+
}
47+
48+
@ParameterizedTest // GH-496
49+
@MethodSource(value = {"args"})
50+
void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) {
51+
52+
LdapRepositoriesRegistrar registrar = new LdapRepositoriesRegistrar();
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[]{"dummyLdapRepository"}),
65+
Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class),
66+
new String[]{"dummyLdapREPO"}));
67+
}
68+
69+
@EnableLdapRepositories(basePackageClasses = DummyLdapRepository.class)
70+
private class Config {
71+
72+
}
73+
74+
@EnableLdapRepositories(basePackageClasses = DummyLdapRepository.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+
}

0 commit comments

Comments
 (0)