From 1edb0cb410c62ff3ebb704fd872a68c841231327 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 31 Jul 2024 14:28:52 +0200 Subject: [PATCH 1/2] Prepare issue branch. --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index d129d27346..5a6771c9e1 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-redis - 3.4.0-SNAPSHOT + 3.4.x-GH-2951-SNAPSHOT Spring Data Redis Spring Data module for Redis @@ -18,7 +18,7 @@ - 3.4.0-SNAPSHOT + 3.4.x-GH-586-SNAPSHOT 3.4.0-SNAPSHOT 4.0.2 1.9.4 From 8b3e3f6708574ed98b94853fedb34505ad24436a Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 31 Jul 2024 15:09:50 +0200 Subject: [PATCH 2/2] Add support for using custom BeanNameGenerator. Closes: #2951 --- .../EnableRedisRepositories.java | 8 ++ .../RedisRepositoriesRegistrarUnitTests.java | 95 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/test/java/org/springframework/data/redis/repository/configuration/RedisRepositoriesRegistrarUnitTests.java diff --git a/src/main/java/org/springframework/data/redis/repository/configuration/EnableRedisRepositories.java b/src/main/java/org/springframework/data/redis/repository/configuration/EnableRedisRepositories.java index eea89db92b..54058c38ea 100644 --- a/src/main/java/org/springframework/data/redis/repository/configuration/EnableRedisRepositories.java +++ b/src/main/java/org/springframework/data/redis/repository/configuration/EnableRedisRepositories.java @@ -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.keyvalue.core.KeyValueOperations; @@ -126,6 +127,13 @@ */ Class repositoryBaseClass() default DefaultRepositoryBaseClass.class; + /** + * Configure a specific {@link BeanNameGenerator} to be used when creating the repositoy beans. + * @return the {@link BeanNameGenerator} to be used or the base {@link BeanNameGenerator} interface to indicate context default. + * @since 3.4 + */ + Class nameGenerator() default BeanNameGenerator.class; + /** * Configures the name of the {@link KeyValueOperations} bean to be used with the repositories detected. * diff --git a/src/test/java/org/springframework/data/redis/repository/configuration/RedisRepositoriesRegistrarUnitTests.java b/src/test/java/org/springframework/data/redis/repository/configuration/RedisRepositoriesRegistrarUnitTests.java new file mode 100644 index 0000000000..7e3d27fbf3 --- /dev/null +++ b/src/test/java/org/springframework/data/redis/repository/configuration/RedisRepositoriesRegistrarUnitTests.java @@ -0,0 +1,95 @@ +/* + * 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.redis.repository.configuration; + +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.redis.core.RedisHash; +import org.springframework.data.repository.CrudRepository; + +/** + * @author Christoph Strobl + */ +class RedisRepositoriesRegistrarUnitTests { + + private BeanDefinitionRegistry registry; + + @BeforeEach + void setUp() { + registry = new DefaultListableBeanFactory(); + } + + @ParameterizedTest // GH-499, GH-3440 + @MethodSource(value = { "args" }) + void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) { + + RedisRepositoriesRegistrar registrar = new RedisRepositoriesRegistrar(); + registrar.setResourceLoader(new DefaultResourceLoader()); + registrar.setEnvironment(new StandardEnvironment()); + registrar.registerBeanDefinitions(metadata, registry); + + Iterable names = Arrays.asList(registry.getBeanDefinitionNames()); + assertThat(names).contains(beanNames); + } + + static Stream args() { + return Stream.of( + Arguments.of(AnnotationMetadata.introspect(Config.class), + new String[] { "redisRepositoriesRegistrarUnitTests.PersonRepository" }), + Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class), + new String[] { "redisRepositoriesRegistrarUnitTests.PersonREPO" })); + } + + @EnableRedisRepositories(basePackageClasses = PersonRepository.class, considerNestedRepositories = true) + private class Config { + + } + + @EnableRedisRepositories(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 { + + } + + @RedisHash + static class Person {} +}