46
46
import org .springframework .core .log .LogMessage ;
47
47
import org .springframework .core .metrics .ApplicationStartup ;
48
48
import org .springframework .core .metrics .StartupStep ;
49
+ import org .springframework .data .repository .config .RepositoryConfigurationPostProcessor .FallbackRepositoryConfigurationPostProcessor ;
49
50
import org .springframework .data .repository .core .support .RepositoryFactorySupport ;
50
51
import org .springframework .lang .Nullable ;
51
52
import org .springframework .util .Assert ;
@@ -83,11 +84,11 @@ public class RepositoryConfigurationDelegate {
83
84
* {@link ResourceLoader} and {@link Environment}.
84
85
*
85
86
* @param configurationSource must not be {@literal null}.
86
- * @param resourceLoader must not be {@literal null}.
87
- * @param environment must not be {@literal null}.
87
+ * @param resourceLoader must not be {@literal null}.
88
+ * @param environment must not be {@literal null}.
88
89
*/
89
90
public RepositoryConfigurationDelegate (RepositoryConfigurationSource configurationSource ,
90
- ResourceLoader resourceLoader , Environment environment ) {
91
+ ResourceLoader resourceLoader , Environment environment ) {
91
92
92
93
this .isXml = configurationSource instanceof XmlRepositoryConfigurationSource ;
93
94
boolean isAnnotation = configurationSource instanceof AnnotationRepositoryConfigurationSource ;
@@ -106,13 +107,13 @@ public RepositoryConfigurationDelegate(RepositoryConfigurationSource configurati
106
107
* Defaults the environment in case the given one is null. Used as fallback, in case the legacy constructor was
107
108
* invoked.
108
109
*
109
- * @param environment can be {@literal null}.
110
+ * @param environment can be {@literal null}.
110
111
* @param resourceLoader can be {@literal null}.
111
112
* @return the given {@link Environment} if not {@literal null}, a configured {@link Environment}, or a default
112
- * {@link Environment}.
113
+ * {@link Environment}.
113
114
*/
114
115
private static Environment defaultEnvironment (@ Nullable Environment environment ,
115
- @ Nullable ResourceLoader resourceLoader ) {
116
+ @ Nullable ResourceLoader resourceLoader ) {
116
117
117
118
if (environment != null ) {
118
119
return environment ;
@@ -125,14 +126,14 @@ private static Environment defaultEnvironment(@Nullable Environment environment,
125
126
/**
126
127
* Registers the discovered repositories in the given {@link BeanDefinitionRegistry}.
127
128
*
128
- * @param registry {@link BeanDefinitionRegistry} in which to register the repository bean.
129
+ * @param registry {@link BeanDefinitionRegistry} in which to register the repository bean.
129
130
* @param extension {@link RepositoryConfigurationExtension} for the module.
130
131
* @return {@link BeanComponentDefinition}s for all repository bean definitions found.
131
132
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension
132
133
* @see org.springframework.beans.factory.support.BeanDefinitionRegistry
133
134
*/
134
135
public List <BeanComponentDefinition > registerRepositoriesIn (BeanDefinitionRegistry registry ,
135
- RepositoryConfigurationExtension extension ) {
136
+ RepositoryConfigurationExtension extension ) {
136
137
137
138
if (logger .isInfoEnabled ()) {
138
139
logger .info (LogMessage .format ("Bootstrapping Spring Data %s repositories in %s mode." , //
@@ -210,29 +211,52 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist
210
211
watch .getLastTaskTimeMillis (), configurations .size (), extension .getModuleName ()));
211
212
}
212
213
213
- // TODO: AOT Processing -> guard this one with a flag so it's not always present
214
- // TODO: With regard to AOT Processing, perhaps we need to be smart and detect whether "core" AOT components are
215
- // (or rather configuration is) present on the classpath to enable Spring Data AOT component registration.
216
- registerAotComponents (registry , extension , metadataByRepositoryBeanName );
214
+ registerPostProcessorComponents (registry , extension , metadataByRepositoryBeanName );
217
215
218
216
return definitions ;
219
217
}
220
218
221
- private void registerAotComponents (BeanDefinitionRegistry registry , RepositoryConfigurationExtension extension ,
222
- Map <String , RepositoryConfigurationAdapter <?>> metadataByRepositoryBeanName ) {
219
+ private void registerPostProcessorComponents (BeanDefinitionRegistry registry , RepositoryConfigurationExtension extension ,
220
+ Map <String , RepositoryConfigurationAdapter <?>> metadataByRepositoryBeanName ) {
223
221
224
- // module-specific repository aot processor
225
- String repositoryAotProcessorBeanName = String . format ( "data-%s.repository-aot-processor" /* might be duplicate */ ,
226
- extension . getModuleIdentifier ());
222
+ boolean hasPostProcessors = false ;
223
+ List < RepositoryConfigurationPostProcessor > processors = SpringFactoriesLoader . loadFactories ( RepositoryConfigurationPostProcessor . class , resourceLoader . getClassLoader ());
224
+ for ( RepositoryConfigurationPostProcessor processor : processors ) {
227
225
228
- if (!registry .isBeanNameInUse (repositoryAotProcessorBeanName )) {
226
+ if (!processor .supports (extension )) {
227
+ continue ;
228
+ }
229
+
230
+ hasPostProcessors = true ;
231
+ registerRepositoryPostProcessor (registry , extension , processor .getClass ().getName (), metadataByRepositoryBeanName );
232
+ }
233
+
234
+ if (!hasPostProcessors ) {
229
235
230
- BeanDefinitionBuilder repositoryAotProcessor = BeanDefinitionBuilder
231
- .rootBeanDefinition (extension .getRepositoryAotProcessor ()).setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
236
+ List <FallbackRepositoryConfigurationPostProcessor > fallback = SpringFactoriesLoader .loadFactories (FallbackRepositoryConfigurationPostProcessor .class , resourceLoader .getClassLoader ());
232
237
233
- repositoryAotProcessor . addPropertyValue ( "configMap" , metadataByRepositoryBeanName );
238
+ for ( RepositoryConfigurationPostProcessor processor : fallback ) {
234
239
235
- registry .registerBeanDefinition (repositoryAotProcessorBeanName , repositoryAotProcessor .getBeanDefinition ());
240
+ if (!processor .supports (extension )) {
241
+ continue ;
242
+ }
243
+
244
+ registerRepositoryPostProcessor (registry , extension , processor .getClass ().getName (), metadataByRepositoryBeanName );
245
+ }
246
+ }
247
+ }
248
+
249
+ private static void registerRepositoryPostProcessor (BeanDefinitionRegistry registry , RepositoryConfigurationExtension extension , String className , Map <String , RepositoryConfigurationAdapter <?>> metadataByRepositoryBeanName ) {
250
+
251
+ String repositoryProcessorBeanName = String .format ("data-%s.repository-post-processor.%s" ,
252
+ extension .getModuleIdentifier (), className );
253
+
254
+ if (!registry .isBeanNameInUse (repositoryProcessorBeanName )) {
255
+ BeanDefinitionBuilder repositoryPostProcessor = BeanDefinitionBuilder
256
+ .rootBeanDefinition (className ).setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
257
+
258
+ repositoryPostProcessor .addPropertyValue ("configMap" , metadataByRepositoryBeanName );
259
+ registry .registerBeanDefinition (repositoryProcessorBeanName , repositoryPostProcessor .getBeanDefinition ());
236
260
}
237
261
}
238
262
@@ -242,10 +266,10 @@ private void registerAotComponents(BeanDefinitionRegistry registry, RepositoryCo
242
266
* augment the {@link LazyRepositoryInjectionPointResolver}'s configuration if there already is one configured.
243
267
*
244
268
* @param configurations must not be {@literal null}.
245
- * @param registry must not be {@literal null}.
269
+ * @param registry must not be {@literal null}.
246
270
*/
247
271
private static void potentiallyLazifyRepositories (Map <String , RepositoryConfiguration <?>> configurations ,
248
- BeanDefinitionRegistry registry , BootstrapMode mode ) {
272
+ BeanDefinitionRegistry registry , BootstrapMode mode ) {
249
273
250
274
if (!DefaultListableBeanFactory .class .isInstance (registry ) || BootstrapMode .DEFAULT .equals (mode )) {
251
275
return ;
@@ -283,7 +307,7 @@ private static void potentiallyLazifyRepositories(Map<String, RepositoryConfigur
283
307
* scanning.
284
308
*
285
309
* @return {@literal true} if multiple data store repository implementations are present in the application. This
286
- * typically means an Spring application is using more than 1 type of data store.
310
+ * typically means an Spring application is using more than 1 type of data store.
287
311
*/
288
312
private boolean multipleStoresDetected () {
289
313
@@ -333,7 +357,7 @@ public LazyRepositoryInjectionPointResolver(Map<String, RepositoryConfiguration<
333
357
*
334
358
* @param configurations must not be {@literal null}.
335
359
* @return a new {@link LazyRepositoryInjectionPointResolver} that will have its configurations augmented with the
336
- * given ones.
360
+ * given ones.
337
361
*/
338
362
LazyRepositoryInjectionPointResolver withAdditionalConfigurations (
339
363
Map <String , RepositoryConfiguration <?>> configurations ) {
0 commit comments