|
15 | 15 | */ |
16 | 16 | package org.springframework.data.repository.config; |
17 | 17 |
|
| 18 | +import static org.springframework.beans.factory.config.BeanDefinition.*; |
| 19 | + |
18 | 20 | import java.util.List; |
| 21 | +import java.util.Locale; |
19 | 22 | import java.util.Optional; |
20 | 23 | import java.util.stream.Collectors; |
21 | 24 | import java.util.stream.Stream; |
22 | 25 |
|
23 | 26 | import org.apache.commons.logging.Log; |
24 | 27 | import org.apache.commons.logging.LogFactory; |
25 | | - |
| 28 | +import org.springframework.beans.factory.config.RuntimeBeanReference; |
26 | 29 | import org.springframework.beans.factory.support.AbstractBeanDefinition; |
27 | 30 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 31 | +import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; |
28 | 32 | import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
29 | 33 | import org.springframework.core.env.Environment; |
30 | 34 | import org.springframework.core.io.ResourceLoader; |
|
35 | 39 | import org.springframework.data.repository.core.support.RepositoryFragmentsFactoryBean; |
36 | 40 | import org.springframework.data.util.Optionals; |
37 | 41 | import org.springframework.util.Assert; |
| 42 | +import org.springframework.util.ClassUtils; |
38 | 43 |
|
39 | 44 | /** |
40 | 45 | * Builder to create {@link BeanDefinitionBuilder} instance to eventually create Spring Data repository instances. |
@@ -119,17 +124,8 @@ public BeanDefinitionBuilder build(RepositoryConfiguration<?> configuration) { |
119 | 124 | builder.addDependsOn(it); |
120 | 125 | }); |
121 | 126 |
|
122 | | - BeanDefinitionBuilder fragmentsBuilder = BeanDefinitionBuilder |
123 | | - .rootBeanDefinition(RepositoryFragmentsFactoryBean.class); |
124 | | - |
125 | | - List<String> fragmentBeanNames = registerRepositoryFragmentsImplementation(configuration) // |
126 | | - .map(RepositoryFragmentConfiguration::getFragmentBeanName) // |
127 | | - .collect(Collectors.toList()); |
128 | | - |
129 | | - fragmentsBuilder.addConstructorArgValue(fragmentBeanNames); |
130 | | - |
131 | | - builder.addPropertyValue("repositoryFragments", |
132 | | - ParsingUtils.getSourceBeanDefinition(fragmentsBuilder, configuration.getSource())); |
| 127 | + String fragmentsBeanName = registerRepositoryFragments(configuration); |
| 128 | + builder.addPropertyValue("repositoryFragments", new RuntimeBeanReference(fragmentsBeanName)); |
133 | 129 |
|
134 | 130 | return builder; |
135 | 131 | } |
@@ -161,6 +157,25 @@ private Optional<String> registerCustomImplementation(RepositoryConfiguration<?> |
161 | 157 | }); |
162 | 158 | } |
163 | 159 |
|
| 160 | + private String registerRepositoryFragments(RepositoryConfiguration<?> configuration) { |
| 161 | + |
| 162 | + BeanDefinitionBuilder fragmentsBuilder = BeanDefinitionBuilder |
| 163 | + .rootBeanDefinition(RepositoryFragmentsFactoryBean.class) // |
| 164 | + .setRole(ROLE_INFRASTRUCTURE); |
| 165 | + |
| 166 | + List<String> fragmentBeanNames = registerRepositoryFragmentsImplementation(configuration) // |
| 167 | + .map(RepositoryFragmentConfiguration::getFragmentBeanName) // |
| 168 | + .collect(Collectors.toList()); |
| 169 | + |
| 170 | + fragmentsBuilder.addConstructorArgValue(fragmentBeanNames); |
| 171 | + |
| 172 | + String fragmentsBeanName = BeanDefinitionReaderUtils |
| 173 | + .uniqueBeanName(String.format("%s.%s.fragments", extension.getModuleName().toLowerCase(Locale.ROOT), |
| 174 | + ClassUtils.getShortName(configuration.getRepositoryInterface())), registry); |
| 175 | + registry.registerBeanDefinition(fragmentsBeanName, fragmentsBuilder.getBeanDefinition()); |
| 176 | + return fragmentsBeanName; |
| 177 | + } |
| 178 | + |
164 | 179 | private Stream<RepositoryFragmentConfiguration> registerRepositoryFragmentsImplementation( |
165 | 180 | RepositoryConfiguration<?> configuration) { |
166 | 181 |
|
|
0 commit comments