Skip to content

Commit ed290da

Browse files
committed
DATACMNS-891 - Repository bean definition registration now exposes repository type in metadata.
We now set an explicit attribute on the BeanDefinition created for every repository factory to allow BeanFactoryPostProcessors to inspect those.
1 parent 4039a99 commit ed290da

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class RepositoryConfigurationDelegate {
5050
private static final String MULTIPLE_MODULES = "Multiple Spring Data modules found, entering strict repository configuration mode!";
5151
private static final String MODULE_DETECTION_PACKAGE = "org.springframework.data.**.repository.support";
5252

53+
static final String FACTORY_BEAN_OBJECT_TYPE = "factoryBeanObjectType";
54+
5355
private final RepositoryConfigurationSource configurationSource;
5456
private final ResourceLoader resourceLoader;
5557
private final Environment environment;
@@ -140,6 +142,8 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist
140142
configuration.getRepositoryInterface(), extension.getRepositoryFactoryClassName());
141143
}
142144

145+
beanDefinition.setAttribute(FACTORY_BEAN_OBJECT_TYPE, configuration.getRepositoryInterface());
146+
143147
registry.registerBeanDefinition(beanName, beanDefinition);
144148
definitions.add(new BeanComponentDefinition(beanDefinition, beanName));
145149
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2016 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+
* http://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.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.Mock;
24+
import org.mockito.runners.MockitoJUnitRunner;
25+
import org.springframework.beans.factory.config.BeanDefinition;
26+
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
27+
import org.springframework.context.support.GenericApplicationContext;
28+
import org.springframework.core.env.StandardEnvironment;
29+
import org.springframework.core.type.StandardAnnotationMetadata;
30+
import org.springframework.data.repository.sample.ProductRepository;
31+
32+
/**
33+
* Unit tests for {@link RepositoryConfigurationDelegate}.
34+
*
35+
* @author Oliver Gierke
36+
* @soundtrack Richard Spaven - Tribute (Whole Other*)
37+
*/
38+
@RunWith(MockitoJUnitRunner.class)
39+
public class RepositoryConfigurationDelegateUnitTests {
40+
41+
@Mock RepositoryConfigurationExtension extension;
42+
43+
/**
44+
* @see DATACMNS-892
45+
*/
46+
@Test
47+
public void registersRepositoryBeanNameAsAttribute() {
48+
49+
StandardEnvironment environment = new StandardEnvironment();
50+
GenericApplicationContext context = new GenericApplicationContext();
51+
RepositoryConfigurationSource configSource = new AnnotationRepositoryConfigurationSource(
52+
new StandardAnnotationMetadata(TestConfig.class, true), EnableRepositories.class, context, environment);
53+
54+
RepositoryConfigurationDelegate delegate = new RepositoryConfigurationDelegate(configSource, context, environment);
55+
56+
for (BeanComponentDefinition definition : delegate.registerRepositoriesIn(context, extension)) {
57+
58+
BeanDefinition beanDefinition = definition.getBeanDefinition();
59+
60+
assertThat(beanDefinition.getAttribute(RepositoryConfigurationDelegate.FACTORY_BEAN_OBJECT_TYPE).toString(),
61+
endsWith("Repository"));
62+
}
63+
}
64+
65+
@EnableRepositories(basePackageClasses = ProductRepository.class)
66+
static class TestConfig {}
67+
}

0 commit comments

Comments
 (0)