From ba12cd9fa3864db45a03a235519037a9e97e2622 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 15 Oct 2019 10:34:43 +0200 Subject: [PATCH 1/3] DATACMNS-1591 - Prepare feature branch --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6a81501ac0..f2bde14ab2 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-commons - 2.3.0.BUILD-SNAPSHOT + 2.3.0.DATACMNS-1591-SNAPSHOT Spring Data Core From 5fc74f448c66cb55ab89b6cfe8bb34be9ba81502 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 15 Oct 2019 10:33:30 +0200 Subject: [PATCH 2/3] DATACMNS-1591 - @Primary is now considered on repository interfaces. We now elevate the primary annotation from the scanned repository bean definition to the one created for the repository factory. This previously got lost as the former is hidden behind the RepositoryConfiguration interface that previously didn't expose the primary nature of the underlying bean definition. Original pull request: #410. --- .../DefaultRepositoryConfiguration.java | 9 +++ .../config/RepositoryConfiguration.java | 7 +++ .../RepositoryConfigurationDelegate.java | 2 + .../PrimaryRepositoryIntegrationTests.java | 55 +++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 src/test/java/org/springframework/data/repository/config/PrimaryRepositoryIntegrationTests.java diff --git a/src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java b/src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java index 3129d8448d..4edc5f3c7b 100644 --- a/src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java +++ b/src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java @@ -205,4 +205,13 @@ public ImplementationLookupConfiguration toLookupConfiguration(MetadataReaderFac return toImplementationDetectionConfiguration(factory).forRepositoryConfiguration(this); } + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.config.RepositoryConfiguration#isPrimary() + */ + @Override + public boolean isPrimary() { + return definition.isPrimary(); + } } diff --git a/src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java b/src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java index 6988395394..8b728fefc8 100644 --- a/src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java +++ b/src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java @@ -130,4 +130,11 @@ public interface RepositoryConfiguration registerRepositoriesIn(BeanDefinitionRegist } AbstractBeanDefinition beanDefinition = definitionBuilder.getBeanDefinition(); + beanDefinition.setPrimary(configuration.isPrimary()); + String beanName = configurationSource.generateBeanName(beanDefinition); if (LOG.isTraceEnabled()) { diff --git a/src/test/java/org/springframework/data/repository/config/PrimaryRepositoryIntegrationTests.java b/src/test/java/org/springframework/data/repository/config/PrimaryRepositoryIntegrationTests.java new file mode 100644 index 0000000000..2121038c6e --- /dev/null +++ b/src/test/java/org/springframework/data/repository/config/PrimaryRepositoryIntegrationTests.java @@ -0,0 +1,55 @@ +/* + * Copyright 2019 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.repository.config; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; +import org.springframework.context.annotation.Primary; +import org.springframework.data.repository.CrudRepository; + +/** + * Integration tests for DATACMNS-1591. + * + * @author Oliver Drotbohm + */ +public class PrimaryRepositoryIntegrationTests { + + @Test // DATACMNS-1591 + public void returnsPrimaryInstance() { + + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); + + // Two beans available but FirstRepository is the primary one + assertThatCode(() -> context.getBean(FirstRepository.class)).doesNotThrowAnyException(); + } + + @Configuration + @EnableRepositories(considerNestedRepositories = true, // + includeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Marker.class)) + static class Config {} + + interface Marker {} + + @Primary + interface FirstRepository extends CrudRepository, Marker {} + + interface SecondRepository extends FirstRepository, Marker {} +} From 84ec3030c8306b79bc7cefa71367ea8c9b885ded Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 15 Oct 2019 10:33:47 +0200 Subject: [PATCH 3/3] DATACMNS-1591 - Polishing. Whitespace. Original pull request: #410. --- .../repository/config/DefaultRepositoryConfiguration.java | 4 ++-- .../data/repository/config/RepositoryConfiguration.java | 2 +- .../repository/config/RepositoryConfigurationDelegate.java | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java b/src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java index 4edc5f3c7b..bac185780e 100644 --- a/src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java +++ b/src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java @@ -182,7 +182,7 @@ public Streamable getExcludeFilters() { return configurationSource.getExcludeFilters(); } - /* + /* * (non-Javadoc) * @see org.springframework.data.repository.config.RepositoryConfiguration#toImplementationDetectionConfiguration(org.springframework.core.type.classreading.MetadataReaderFactory) */ @@ -194,7 +194,7 @@ public ImplementationDetectionConfiguration toImplementationDetectionConfigurati return configurationSource.toImplementationDetectionConfiguration(factory); } - /* + /* * (non-Javadoc) * @see org.springframework.data.repository.config.RepositoryConfiguration#toLookupConfiguration(org.springframework.core.type.classreading.MetadataReaderFactory) */ diff --git a/src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java b/src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java index 8b728fefc8..59d68eac3a 100644 --- a/src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java +++ b/src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java @@ -124,7 +124,7 @@ public interface RepositoryConfiguration registerRepositoriesIn(BeanDefinitionRegist * Registers a {@link LazyRepositoryInjectionPointResolver} over the default * {@link ContextAnnotationAutowireCandidateResolver} to make injection points of lazy repositories lazy, too. Will * augment the {@link LazyRepositoryInjectionPointResolver}'s configuration if there already is one configured. - * + * * @param configurations must not be {@literal null}. * @param registry must not be {@literal null}. */ @@ -265,7 +265,7 @@ static class LazyRepositoryInjectionPointResolver extends ContextAnnotationAutow /** * Returns a new {@link LazyRepositoryInjectionPointResolver} that will have its configurations augmented with the * given ones. - * + * * @param configurations must not be {@literal null}. * @return */ @@ -278,7 +278,7 @@ LazyRepositoryInjectionPointResolver withAdditionalConfigurations( return new LazyRepositoryInjectionPointResolver(map); } - /* + /* * (non-Javadoc) * @see org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver#isLazy(org.springframework.beans.factory.config.DependencyDescriptor) */