Skip to content

Commit 4024b2f

Browse files
committed
DefaultListableBeanFactory leniently deserializes into dummy factory if serialization id not resolvable
Issue: SPR-14117
1 parent 6db6f23 commit 4024b2f

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,16 +1449,14 @@ public SerializedBeanFactoryReference(String id) {
14491449

14501450
private Object readResolve() {
14511451
Reference<?> ref = serializableFactories.get(this.id);
1452-
if (ref == null) {
1453-
throw new IllegalStateException(
1454-
"Cannot deserialize BeanFactory with id " + this.id + ": no factory registered for this id");
1455-
}
1456-
Object result = ref.get();
1457-
if (result == null) {
1458-
throw new IllegalStateException(
1459-
"Cannot deserialize BeanFactory with id " + this.id + ": factory has been garbage-collected");
1452+
if (ref != null) {
1453+
Object result = ref.get();
1454+
if (result != null) {
1455+
return result;
1456+
}
14601457
}
1461-
return result;
1458+
// Lenient fallback: dummy factory in case of original factory not found...
1459+
return new StaticListableBeanFactory(Collections.<String, Object> emptyMap());
14621460
}
14631461
}
14641462

spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.lang.annotation.Annotation;
2020
import java.util.ArrayList;
21-
import java.util.HashMap;
2221
import java.util.LinkedHashMap;
2322
import java.util.List;
2423
import java.util.Map;
@@ -35,6 +34,7 @@
3534
import org.springframework.beans.factory.SmartFactoryBean;
3635
import org.springframework.core.ResolvableType;
3736
import org.springframework.core.annotation.AnnotationUtils;
37+
import org.springframework.util.Assert;
3838
import org.springframework.util.StringUtils;
3939

4040
/**
@@ -59,7 +59,31 @@
5959
public class StaticListableBeanFactory implements ListableBeanFactory {
6060

6161
/** Map from bean name to bean instance */
62-
private final Map<String, Object> beans = new HashMap<String, Object>();
62+
private final Map<String, Object> beans;
63+
64+
65+
/**
66+
* Create a regular {@code StaticListableBeanFactory}, to be populated
67+
* with singleton bean instances through {@link #addBean} calls.
68+
*/
69+
public StaticListableBeanFactory() {
70+
this.beans = new LinkedHashMap<String, Object>();
71+
}
72+
73+
/**
74+
* Create a {@code StaticListableBeanFactory} wrapping the given {@code Map}.
75+
* <p>Note that the given {@code Map} may be pre-populated with beans;
76+
* or new, still allowing for beans to be registered via {@link #addBean};
77+
* or {@link java.util.Collections#emptyMap()} for a dummy factory which
78+
* enforces operating against an empty set of beans.
79+
* @param beans a {@code Map} for holding this factory's beans, with the
80+
* bean name String as key and the corresponding singleton object as value
81+
* @since 4.3
82+
*/
83+
public StaticListableBeanFactory(Map<String, Object> beans) {
84+
Assert.notNull(beans, "Beans Map must not be null");
85+
this.beans = beans;
86+
}
6387

6488

6589
/**
@@ -265,7 +289,7 @@ public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingle
265289
throws BeansException {
266290

267291
boolean isFactoryType = (type != null && FactoryBean.class.isAssignableFrom(type));
268-
Map<String, T> matches = new HashMap<String, T>();
292+
Map<String, T> matches = new LinkedHashMap<String, T>();
269293

270294
for (Map.Entry<String, Object> entry : this.beans.entrySet()) {
271295
String beanName = entry.getKey();

0 commit comments

Comments
 (0)