From d1faa7187d60b8c07d4db8290151ac690960a353 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 12 Feb 2014 10:24:37 +0100 Subject: [PATCH] DATACMNS-432 - Fix regression in Repositories. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The changes in 899cf25 causes Repositories not finding any repository instances in case the lookup of beans by type for RepositoryFactoryInformation if the initialization causes as cyclic reference. Even worse, we might run into cases in which the attempt to access the beans by type causes transitive object creation which results in a successful lookup on a second attempt. This is effectively caused by the catch-block in DefaultListableBeanFactory.getBeansOfType(…) which registers the suppressed exception as the method is used for lookup the beans for injection points with Lists and Maps. We now explicitly look for bean names first and access the bean by name afterwards, which guarantees the exception to be thrown if it occurs. This will reveal the underlying issue and let user potentially deal with it. It's generally recommended to refer to Repositories in a very lazy fashion (using Provider, ObjectFactory or @Lazy on the injection point as of Spring 4) to avoid creating circular dependencies through by-type-lookups. --- .../data/repository/support/Repositories.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/support/Repositories.java b/src/main/java/org/springframework/data/repository/support/Repositories.java index 1682e1696..3753756d6 100644 --- a/src/main/java/org/springframework/data/repository/support/Repositories.java +++ b/src/main/java/org/springframework/data/repository/support/Repositories.java @@ -21,7 +21,6 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryUtils; @@ -82,19 +81,16 @@ public class Repositories implements Iterable> { @SuppressWarnings({ "rawtypes", "unchecked" }) private void populateRepositoryFactoryInformation(ListableBeanFactory factory) { - Set> repositoryFactoryBeans = BeanFactoryUtils - .beansOfTypeIncludingAncestors(factory, RepositoryFactoryInformation.class).entrySet(); + for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, RepositoryFactoryInformation.class, + false, false)) { - for (Map.Entry entry : repositoryFactoryBeans) { - - String beanName = entry.getKey(); - - RepositoryFactoryInformation repositoryFactoryInformation = entry.getValue(); + RepositoryFactoryInformation repositoryFactoryInformation = beanFactory.getBean(name, + RepositoryFactoryInformation.class); Class userDomainType = ClassUtils.getUserClass(repositoryFactoryInformation.getRepositoryInformation() .getDomainType()); this.repositoryFactoryInfos.put(userDomainType, repositoryFactoryInformation); - this.repositoryBeanNames.put(userDomainType, BeanFactoryUtils.transformedBeanName(beanName)); + this.repositoryBeanNames.put(userDomainType, BeanFactoryUtils.transformedBeanName(name)); } }