Browse Source

DATACMNS-432 - Fix regression in Repositories.

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<T>, ObjectFactory<T> or @Lazy on the injection point as of Spring 4) to avoid creating circular dependencies through by-type-lookups.
pull/64/head
Oliver Gierke 12 years ago
parent
commit
d1faa7187d
  1. 14
      src/main/java/org/springframework/data/repository/support/Repositories.java

14
src/main/java/org/springframework/data/repository/support/Repositories.java

@ -21,7 +21,6 @@ import java.util.HashMap; @@ -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<Class<?>> { @@ -82,19 +81,16 @@ public class Repositories implements Iterable<Class<?>> {
@SuppressWarnings({ "rawtypes", "unchecked" })
private void populateRepositoryFactoryInformation(ListableBeanFactory factory) {
Set<Map.Entry<String, RepositoryFactoryInformation>> repositoryFactoryBeans = BeanFactoryUtils
.beansOfTypeIncludingAncestors(factory, RepositoryFactoryInformation.class).entrySet();
for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, RepositoryFactoryInformation.class,
false, false)) {
for (Map.Entry<String, RepositoryFactoryInformation> 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));
}
}

Loading…
Cancel
Save