Skip to content

Commit f99fb4b

Browse files
odrotbohmmp911de
authored andcommitted
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.
1 parent 2b7c115 commit f99fb4b

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/main/java/org/springframework/data/repository/config/DefaultRepositoryConfiguration.java

+9
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,13 @@ public ImplementationLookupConfiguration toLookupConfiguration(MetadataReaderFac
205205

206206
return toImplementationDetectionConfiguration(factory).forRepositoryConfiguration(this);
207207
}
208+
209+
/*
210+
* (non-Javadoc)
211+
* @see org.springframework.data.repository.config.RepositoryConfiguration#isPrimary()
212+
*/
213+
@Override
214+
public boolean isPrimary() {
215+
return definition.isPrimary();
216+
}
208217
}

src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java

+7
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,11 @@ public interface RepositoryConfiguration<T extends RepositoryConfigurationSource
130130
* @since 2.1
131131
*/
132132
ImplementationLookupConfiguration toLookupConfiguration(MetadataReaderFactory factory);
133+
134+
/**
135+
* Returns whether the repository is the primary one for its type.
136+
*
137+
* @return
138+
*/
139+
boolean isPrimary();
133140
}

src/main/java/org/springframework/data/repository/config/RepositoryConfigurationDelegate.java

+2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist
161161
}
162162

163163
AbstractBeanDefinition beanDefinition = definitionBuilder.getBeanDefinition();
164+
beanDefinition.setPrimary(configuration.isPrimary());
165+
164166
String beanName = configurationSource.generateBeanName(beanDefinition);
165167

166168
if (LOG.isTraceEnabled()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.repository.config;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.Test;
21+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
22+
import org.springframework.context.annotation.ComponentScan.Filter;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.annotation.FilterType;
25+
import org.springframework.context.annotation.Primary;
26+
import org.springframework.data.repository.CrudRepository;
27+
28+
/**
29+
* Integration tests for DATACMNS-1591.
30+
*
31+
* @author Oliver Drotbohm
32+
*/
33+
public class PrimaryRepositoryIntegrationTests {
34+
35+
@Test // DATACMNS-1591
36+
public void returnsPrimaryInstance() {
37+
38+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
39+
40+
// Two beans available but FirstRepository is the primary one
41+
assertThatCode(() -> context.getBean(FirstRepository.class)).doesNotThrowAnyException();
42+
}
43+
44+
@Configuration
45+
@EnableRepositories(considerNestedRepositories = true, //
46+
includeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Marker.class))
47+
static class Config {}
48+
49+
interface Marker {}
50+
51+
@Primary
52+
interface FirstRepository<T> extends CrudRepository<T, Long>, Marker {}
53+
54+
interface SecondRepository extends FirstRepository<Object>, Marker {}
55+
}

0 commit comments

Comments
 (0)