Skip to content

Commit ef61451

Browse files
Add support for using custom BeanNameGenerator.
Closes: #1509
1 parent 728a83b commit ef61451

File tree

4 files changed

+130
-3
lines changed

4 files changed

+130
-3
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/config/EnableCassandraRepositories.java

Lines changed: 8 additions & 0 deletions
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.cassandra.config.DefaultBeanNames;
@@ -110,6 +111,13 @@
110111
*/
111112
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
112113

114+
/**
115+
* Configure a specific {@link BeanNameGenerator} to be used when creating the repository beans.
116+
* @return the {@link BeanNameGenerator} to be used or the base {@link BeanNameGenerator} interface to indicate context default.
117+
* @since 4.4
118+
*/
119+
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
120+
113121
/**
114122
* Configures the name of the {@link CassandraTemplate} bean to be used with the repositories detected. Defaults to
115123
* {@link DefaultBeanNames#DATA_TEMPLATE}

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/config/EnableReactiveCassandraRepositories.java

Lines changed: 9 additions & 0 deletions
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.cassandra.repository.support.ReactiveCassandraRepositoryFactoryBean;
@@ -36,6 +37,7 @@
3637
* annotated class.
3738
*
3839
* @author Mark Paluch
40+
* @author Christoph Strobl
3941
* @since 2.0
4042
*/
4143
@Documented
@@ -106,6 +108,13 @@
106108
*/
107109
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
108110

111+
/**
112+
* Configure a specific {@link BeanNameGenerator} to be used when creating the repository beans.
113+
* @return the {@link BeanNameGenerator} to be used or the base {@link BeanNameGenerator} interface to indicate context default.
114+
* @since 4.4
115+
*/
116+
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
117+
109118
/**
110119
* Configures the name of the {@link org.springframework.data.cassandra.core.ReactiveCassandraTemplate} bean to be
111120
* used with the repositories detected.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.cassandra.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.repository.CrudRepository;
35+
36+
/**
37+
* @author Christoph Strobl
38+
*/
39+
public class CassandraRepositoriesRegistrarUnitTests {
40+
41+
private BeanDefinitionRegistry registry;
42+
43+
@BeforeEach
44+
void setUp() {
45+
registry = new DefaultListableBeanFactory();
46+
}
47+
48+
@ParameterizedTest // GH-1509
49+
@MethodSource(value = {"args"})
50+
void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) {
51+
52+
CassandraRepositoriesRegistrar registrar = new CassandraRepositoriesRegistrar();
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[]{"cassandraRepositoriesRegistrarUnitTests.PersonRepository"}),
65+
Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class),
66+
new String[]{"cassandraRepositoriesRegistrarUnitTests.PersonREPO"}));
67+
}
68+
69+
@EnableCassandraRepositories(basePackageClasses = PersonRepository.class, considerNestedRepositories = true)
70+
private class Config {
71+
72+
}
73+
74+
@EnableCassandraRepositories(basePackageClasses = PersonRepository.class, nameGenerator = MyBeanNameGenerator.class, considerNestedRepositories = true)
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+
87+
interface PersonRepository extends CrudRepository<Person, String> {
88+
89+
}
90+
91+
class Person {
92+
93+
}
94+
}

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/repository/config/ReactiveCassandraRepositoriesRegistrarUnitTests.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
*/
1616
package org.springframework.data.cassandra.repository.config;
1717

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

2021
import org.junit.jupiter.api.Test;
2122
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.beans.factory.config.BeanDefinition;
24+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2225
import org.springframework.context.ApplicationContext;
26+
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
2327
import org.springframework.context.annotation.Bean;
2428
import org.springframework.context.annotation.ComponentScan.Filter;
2529
import org.springframework.context.annotation.Configuration;
@@ -37,13 +41,15 @@
3741
* Unit tests for {@link ReactiveCassandraRepositoriesRegistrar}.
3842
*
3943
* @author Mark Paluch
44+
* @author Christoph Strobl
4045
*/
4146
@SpringJUnitConfig
4247
public class ReactiveCassandraRepositoriesRegistrarUnitTests {
4348

4449
@Configuration
4550
@EnableReactiveCassandraRepositories(basePackages = "org.springframework.data.cassandra.repository.config",
4651
considerNestedRepositories = true,
52+
nameGenerator = MyBeanNameGenerator.class,
4753
includeFilters = @Filter(pattern = ".*ReactivePersonRepository", type = FilterType.REGEX))
4854
static class Config {
4955

@@ -61,8 +67,18 @@ public ReactiveCassandraTemplate reactiveCassandraTemplate() throws Exception {
6167
@Autowired ApplicationContext context;
6268
@Autowired ReactivePersonRepository personRepository;
6369

64-
@Test // DATACASS-335
65-
void testConfiguration() {}
70+
@Test // DATACASS-335, GH-1509
71+
void testConfiguration() {
72+
assertThat(context.containsBean("reactiveCassandraRepositoriesRegistrarUnitTests.ReactivePersonREPO")).isTrue();
73+
}
6674

6775
interface ReactivePersonRepository extends ReactiveCassandraRepository<Person, String> {}
76+
77+
static class MyBeanNameGenerator extends AnnotationBeanNameGenerator {
78+
79+
@Override
80+
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
81+
return super.generateBeanName(definition, registry).replaceAll("Repository", "REPO");
82+
}
83+
}
6884
}

0 commit comments

Comments
 (0)