Skip to content

Add support for using custom BeanNameGenerator. #3551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-3440-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-3440-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-3440-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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.Lazy;
import org.springframework.core.annotation.AliasFor;
Expand Down Expand Up @@ -136,6 +137,15 @@
@AliasFor(annotation = EnableJpaRepositories.class)
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
* @see EnableJpaRepositories#nameGenerator()
*/
@AliasFor(annotation = EnableJpaRepositories.class)
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

// JPA specific configuration

/**
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-3440-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-performance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-3440-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-3440-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-3440-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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.context.annotation.Lazy;
Expand Down Expand Up @@ -119,6 +120,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 3.4
*/
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

// JPA specific configuration

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
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.api.Test;
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;
Expand All @@ -34,33 +39,54 @@
* @author Oliver Gierke
* @author Jens Schauder
* @author Erik Pellizzon
* @author Christoph Strobl
*/
class JpaRepositoriesRegistrarUnitTests {

private BeanDefinitionRegistry registry;
private AnnotationMetadata metadata;

@BeforeEach
void setUp() {

metadata = AnnotationMetadata.introspect(Config.class);
registry = new DefaultListableBeanFactory();
}

@Test
void configuresRepositoriesCorrectly() {

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

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

Iterable<String> names = Arrays.asList(registry.getBeanDefinitionNames());
assertThat(names).contains("userRepository", "auditableUserRepository", "roleRepository");
assertThat(names).contains(beanNames);
}

static Stream<Arguments> args() {
return Stream.of(
Arguments.of(AnnotationMetadata.introspect(Config.class),
new String[] { "userRepository", "auditableUserRepository", "roleRepository" }),
Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class),
new String[] { "userREPO", "auditableUserREPO", "roleREPO" }));
}

@EnableJpaRepositories(basePackageClasses = UserRepository.class)
private class Config {

}

@EnableJpaRepositories(basePackageClasses = UserRepository.class, nameGenerator = MyBeanNameGenerator.class)
private class ConfigWithBeanNameGenerator {

}

static class MyBeanNameGenerator extends AnnotationBeanNameGenerator {

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