Skip to content

Commit

Permalink
Add support for using custom BeanNameGenerator.
Browse files Browse the repository at this point in the history
Closes: #1509
  • Loading branch information
christophstrobl committed Aug 1, 2024
1 parent 5b8c6cd commit 8655627
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.lang.annotation.Target;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Import;
import org.springframework.data.cassandra.config.DefaultBeanNames;
Expand Down Expand Up @@ -110,6 +111,13 @@
*/
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;

/**
* Configure a specific {@link BeanNameGenerator} to be used when creating the repository beans.
* @return the {@link BeanNameGenerator} to be used or the base {@link BeanNameGenerator} interface to indicate context default.
* @since 4.4
*/
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

/**
* Configures the name of the {@link CassandraTemplate} bean to be used with the repositories detected. Defaults to
* {@link DefaultBeanNames#DATA_TEMPLATE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.lang.annotation.Target;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Import;
import org.springframework.data.cassandra.repository.support.ReactiveCassandraRepositoryFactoryBean;
Expand All @@ -36,6 +37,7 @@
* annotated class.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.0
*/
@Documented
Expand Down Expand Up @@ -106,6 +108,13 @@
*/
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;

/**
* Configure a specific {@link BeanNameGenerator} to be used when creating the repository beans.
* @return the {@link BeanNameGenerator} to be used or the base {@link BeanNameGenerator} interface to indicate context default.
* @since 4.4
*/
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

/**
* Configures the name of the {@link org.springframework.data.cassandra.core.ReactiveCassandraTemplate} bean to be
* used with the repositories detected.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.repository.config;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.data.repository.CrudRepository;

/**
* @author Christoph Strobl
*/
public class CassandraRepositoriesRegistrarUnitTests {

private BeanDefinitionRegistry registry;

@BeforeEach
void setUp() {
registry = new DefaultListableBeanFactory();
}

@ParameterizedTest // GH-1509
@MethodSource(value = {"args"})
void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) {

CassandraRepositoriesRegistrar registrar = new CassandraRepositoriesRegistrar();
registrar.setResourceLoader(new DefaultResourceLoader());
registrar.setEnvironment(new StandardEnvironment());
registrar.registerBeanDefinitions(metadata, registry);

Iterable<String> names = Arrays.asList(registry.getBeanDefinitionNames());
assertThat(names).contains(beanNames);
}

static Stream<Arguments> args() {
return Stream.of(
Arguments.of(AnnotationMetadata.introspect(Config.class),
new String[]{"cassandraRepositoriesRegistrarUnitTests.PersonRepository"}),
Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class),
new String[]{"cassandraRepositoriesRegistrarUnitTests.PersonREPO"}));
}

@EnableCassandraRepositories(basePackageClasses = PersonRepository.class, considerNestedRepositories = true)
private class Config {

}

@EnableCassandraRepositories(basePackageClasses = PersonRepository.class, nameGenerator = MyBeanNameGenerator.class, considerNestedRepositories = true)
private class ConfigWithBeanNameGenerator {

}

static class MyBeanNameGenerator extends AnnotationBeanNameGenerator {

@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
return super.generateBeanName(definition, registry).replaceAll("Repository", "REPO");
}
}

interface PersonRepository extends CrudRepository<Person, String> {

}

class Person {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
*/
package org.springframework.data.cassandra.repository.config;

import static org.mockito.Mockito.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
Expand All @@ -37,13 +41,15 @@
* Unit tests for {@link ReactiveCassandraRepositoriesRegistrar}.
*
* @author Mark Paluch
* @author Christoph Strobl
*/
@SpringJUnitConfig
public class ReactiveCassandraRepositoriesRegistrarUnitTests {

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

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

@Test // DATACASS-335
void testConfiguration() {}
@Test // DATACASS-335, GH-1509
void testConfiguration() {
assertThat(context.containsBean("reactiveCassandraRepositoriesRegistrarUnitTests.ReactivePersonREPO")).isTrue();
}

interface ReactivePersonRepository extends ReactiveCassandraRepository<Person, String> {}

static class MyBeanNameGenerator extends AnnotationBeanNameGenerator {

@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
return super.generateBeanName(definition, registry).replaceAll("Repository", "REPO");
}
}
}

0 comments on commit 8655627

Please # to comment.