From 4024b2fd4bde02716cd51ab34c145ca7bfcd499f Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 5 Apr 2016 15:49:30 +0200 Subject: [PATCH] DefaultListableBeanFactory leniently deserializes into dummy factory if serialization id not resolvable Issue: SPR-14117 --- .../support/DefaultListableBeanFactory.java | 16 ++++------ .../support/StaticListableBeanFactory.java | 32 ++++++++++++++++--- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 7d6d6b2e9cd..4516cc5f01c 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -1449,16 +1449,14 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto private Object readResolve() { Reference ref = serializableFactories.get(this.id); - if (ref == null) { - throw new IllegalStateException( - "Cannot deserialize BeanFactory with id " + this.id + ": no factory registered for this id"); + if (ref != null) { + Object result = ref.get(); + if (result != null) { + return result; + } } - Object result = ref.get(); - if (result == null) { - throw new IllegalStateException( - "Cannot deserialize BeanFactory with id " + this.id + ": factory has been garbage-collected"); - } - return result; + // Lenient fallback: dummy factory in case of original factory not found... + return new StaticListableBeanFactory(Collections. emptyMap()); } } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java index 2d701ce7fac..32866846dee 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package org.springframework.beans.factory.support; import java.lang.annotation.Annotation; import java.util.ArrayList; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -35,6 +34,7 @@ import org.springframework.beans.factory.NoUniqueBeanDefinitionException; import org.springframework.beans.factory.SmartFactoryBean; import org.springframework.core.ResolvableType; import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** @@ -59,7 +59,31 @@ import org.springframework.util.StringUtils; public class StaticListableBeanFactory implements ListableBeanFactory { /** Map from bean name to bean instance */ - private final Map beans = new HashMap(); + private final Map beans; + + + /** + * Create a regular {@code StaticListableBeanFactory}, to be populated + * with singleton bean instances through {@link #addBean} calls. + */ + public StaticListableBeanFactory() { + this.beans = new LinkedHashMap(); + } + + /** + * Create a {@code StaticListableBeanFactory} wrapping the given {@code Map}. + *

Note that the given {@code Map} may be pre-populated with beans; + * or new, still allowing for beans to be registered via {@link #addBean}; + * or {@link java.util.Collections#emptyMap()} for a dummy factory which + * enforces operating against an empty set of beans. + * @param beans a {@code Map} for holding this factory's beans, with the + * bean name String as key and the corresponding singleton object as value + * @since 4.3 + */ + public StaticListableBeanFactory(Map beans) { + Assert.notNull(beans, "Beans Map must not be null"); + this.beans = beans; + } /** @@ -265,7 +289,7 @@ public class StaticListableBeanFactory implements ListableBeanFactory { throws BeansException { boolean isFactoryType = (type != null && FactoryBean.class.isAssignableFrom(type)); - Map matches = new HashMap(); + Map matches = new LinkedHashMap(); for (Map.Entry entry : this.beans.entrySet()) { String beanName = entry.getKey();