Skip to content

DATACMNS-1591 - @Primary is now considered on repository interfaces #410

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 3 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-commons</artifactId>
<version>2.3.0.BUILD-SNAPSHOT</version>
<version>2.3.0.DATACMNS-1591-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public Streamable<TypeFilter> getExcludeFilters() {
return configurationSource.getExcludeFilters();
}

/*
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfiguration#toImplementationDetectionConfiguration(org.springframework.core.type.classreading.MetadataReaderFactory)
*/
Expand All @@ -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)
*/
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,17 @@ public interface RepositoryConfiguration<T extends RepositoryConfigurationSource

/**
* Returns the {@link ImplementationLookupConfiguration} for the given {@link MetadataReaderFactory}.
*
*
* @param factory must not be {@literal null}.
* @return will never be {@literal null}.
* @since 2.1
*/
ImplementationLookupConfiguration toLookupConfiguration(MetadataReaderFactory factory);

/**
* Returns whether the repository is the primary one for its type.
*
* @return
*/
boolean isPrimary();
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist
}

AbstractBeanDefinition beanDefinition = definitionBuilder.getBeanDefinition();
beanDefinition.setPrimary(configuration.isPrimary());

String beanName = configurationSource.generateBeanName(beanDefinition);

if (LOG.isTraceEnabled()) {
Expand Down Expand Up @@ -190,7 +192,7 @@ public List<BeanComponentDefinition> 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}.
*/
Expand Down Expand Up @@ -263,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
*/
Expand All @@ -276,7 +278,7 @@ LazyRepositoryInjectionPointResolver withAdditionalConfigurations(
return new LazyRepositoryInjectionPointResolver(map);
}

/*
/*
* (non-Javadoc)
* @see org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver#isLazy(org.springframework.beans.factory.config.DependencyDescriptor)
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> extends CrudRepository<T, Long>, Marker {}

interface SecondRepository extends FirstRepository<Object>, Marker {}
}