Skip to content

Commit 8b3e3f6

Browse files
Add support for using custom BeanNameGenerator.
Closes: #2951
1 parent 1edb0cb commit 8b3e3f6

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

src/main/java/org/springframework/data/redis/repository/configuration/EnableRedisRepositories.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.keyvalue.core.KeyValueOperations;
@@ -126,6 +127,13 @@
126127
*/
127128
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
128129

130+
/**
131+
* Configure a specific {@link BeanNameGenerator} to be used when creating the repositoy beans.
132+
* @return the {@link BeanNameGenerator} to be used or the base {@link BeanNameGenerator} interface to indicate context default.
133+
* @since 3.4
134+
*/
135+
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
136+
129137
/**
130138
* Configures the name of the {@link KeyValueOperations} bean to be used with the repositories detected.
131139
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.redis.repository.configuration;
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.redis.core.RedisHash;
35+
import org.springframework.data.repository.CrudRepository;
36+
37+
/**
38+
* @author Christoph Strobl
39+
*/
40+
class RedisRepositoriesRegistrarUnitTests {
41+
42+
private BeanDefinitionRegistry registry;
43+
44+
@BeforeEach
45+
void setUp() {
46+
registry = new DefaultListableBeanFactory();
47+
}
48+
49+
@ParameterizedTest // GH-499, GH-3440
50+
@MethodSource(value = { "args" })
51+
void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) {
52+
53+
RedisRepositoriesRegistrar registrar = new RedisRepositoriesRegistrar();
54+
registrar.setResourceLoader(new DefaultResourceLoader());
55+
registrar.setEnvironment(new StandardEnvironment());
56+
registrar.registerBeanDefinitions(metadata, registry);
57+
58+
Iterable<String> names = Arrays.asList(registry.getBeanDefinitionNames());
59+
assertThat(names).contains(beanNames);
60+
}
61+
62+
static Stream<Arguments> args() {
63+
return Stream.of(
64+
Arguments.of(AnnotationMetadata.introspect(Config.class),
65+
new String[] { "redisRepositoriesRegistrarUnitTests.PersonRepository" }),
66+
Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class),
67+
new String[] { "redisRepositoriesRegistrarUnitTests.PersonREPO" }));
68+
}
69+
70+
@EnableRedisRepositories(basePackageClasses = PersonRepository.class, considerNestedRepositories = true)
71+
private class Config {
72+
73+
}
74+
75+
@EnableRedisRepositories(basePackageClasses = PersonRepository.class, nameGenerator = MyBeanNameGenerator.class,
76+
considerNestedRepositories = true)
77+
private class ConfigWithBeanNameGenerator {
78+
79+
}
80+
81+
static class MyBeanNameGenerator extends AnnotationBeanNameGenerator {
82+
83+
@Override
84+
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
85+
return super.generateBeanName(definition, registry).replaceAll("Repository", "REPO");
86+
}
87+
}
88+
89+
interface PersonRepository extends CrudRepository<Person, String> {
90+
91+
}
92+
93+
@RedisHash
94+
static class Person {}
95+
}

0 commit comments

Comments
 (0)