27
27
import org .springframework .beans .factory .BeanFactory ;
28
28
import org .springframework .beans .factory .BeanFactoryUtils ;
29
29
import org .springframework .beans .factory .ListableBeanFactory ;
30
+ import org .springframework .beans .factory .config .BeanDefinition ;
31
+ import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
30
32
import org .springframework .data .mapping .PersistentEntity ;
31
33
import org .springframework .data .mapping .context .MappingContext ;
32
34
import org .springframework .data .repository .core .EntityInformation ;
@@ -91,7 +93,7 @@ private void populateRepositoryFactoryInformation(ListableBeanFactory factory) {
91
93
}
92
94
}
93
95
94
- @ SuppressWarnings ({ "rawtypes" , "unchecked" } )
96
+ @ SuppressWarnings ("rawtypes" )
95
97
private synchronized void cacheRepositoryFactory (String name ) {
96
98
97
99
RepositoryFactoryInformation repositoryFactoryInformation = beanFactory .get ().getBean (name ,
@@ -101,15 +103,13 @@ private synchronized void cacheRepositoryFactory(String name) {
101
103
102
104
RepositoryInformation information = repositoryFactoryInformation .getRepositoryInformation ();
103
105
Set <Class <?>> alternativeDomainTypes = information .getAlternativeDomainTypes ();
104
- String beanName = BeanFactoryUtils .transformedBeanName (name );
105
106
106
107
Set <Class <?>> typesToRegister = new HashSet <>(alternativeDomainTypes .size () + 1 );
107
108
typesToRegister .add (domainType );
108
109
typesToRegister .addAll (alternativeDomainTypes );
109
110
110
111
for (Class <?> type : typesToRegister ) {
111
- this .repositoryFactoryInfos .put (type , repositoryFactoryInformation );
112
- this .repositoryBeanNames .put (type , beanName );
112
+ cacheFirstOrPrimary (type , repositoryFactoryInformation , BeanFactoryUtils .transformedBeanName (name ));
113
113
}
114
114
}
115
115
@@ -263,6 +263,35 @@ public Iterator<Class<?>> iterator() {
263
263
return repositoryFactoryInfos .keySet ().iterator ();
264
264
}
265
265
266
+ /**
267
+ * Caches the repository information for the given domain type or overrides existing information in case the bean name
268
+ * points to a primary bean definition.
269
+ *
270
+ * @param type must not be {@literal null}.
271
+ * @param information must not be {@literal null}.
272
+ * @param name must not be {@literal null}.
273
+ */
274
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
275
+ private void cacheFirstOrPrimary (Class <?> type , RepositoryFactoryInformation information , String name ) {
276
+
277
+ if (repositoryBeanNames .containsKey (type )) {
278
+
279
+ Boolean presentAndPrimary = beanFactory //
280
+ .filter (ConfigurableListableBeanFactory .class ::isInstance ) //
281
+ .map (ConfigurableListableBeanFactory .class ::cast ) //
282
+ .map (it -> it .getBeanDefinition (name )) //
283
+ .map (BeanDefinition ::isPrimary ) //
284
+ .orElse (false );
285
+
286
+ if (!presentAndPrimary ) {
287
+ return ;
288
+ }
289
+ }
290
+
291
+ this .repositoryFactoryInfos .put (type , information );
292
+ this .repositoryBeanNames .put (type , name );
293
+ }
294
+
266
295
/**
267
296
* Null-object to avoid nasty {@literal null} checks in cache lookups.
268
297
*
0 commit comments