|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.lang.annotation.Annotation;
|
20 | 20 | import java.util.ArrayList;
|
21 |
| -import java.util.HashMap; |
22 | 21 | import java.util.LinkedHashMap;
|
23 | 22 | import java.util.List;
|
24 | 23 | import java.util.Map;
|
|
35 | 34 | import org.springframework.beans.factory.SmartFactoryBean;
|
36 | 35 | import org.springframework.core.ResolvableType;
|
37 | 36 | import org.springframework.core.annotation.AnnotationUtils;
|
| 37 | +import org.springframework.util.Assert; |
38 | 38 | import org.springframework.util.StringUtils;
|
39 | 39 |
|
40 | 40 | /**
|
|
59 | 59 | public class StaticListableBeanFactory implements ListableBeanFactory {
|
60 | 60 |
|
61 | 61 | /** 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 | + } |
63 | 87 |
|
64 | 88 |
|
65 | 89 | /**
|
@@ -265,7 +289,7 @@ public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingle
|
265 | 289 | throws BeansException {
|
266 | 290 |
|
267 | 291 | 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>(); |
269 | 293 |
|
270 | 294 | for (Map.Entry<String, Object> entry : this.beans.entrySet()) {
|
271 | 295 | String beanName = entry.getKey();
|
|
0 commit comments