|
16 | 16 |
|
17 | 17 | package org.springframework.test.context.bean.override;
|
18 | 18 |
|
19 |
| -import java.lang.reflect.Field; |
20 | 19 | import java.util.LinkedHashMap;
|
21 | 20 | import java.util.List;
|
22 | 21 | import java.util.Map;
|
|
25 | 24 | import org.apache.commons.logging.Log;
|
26 | 25 | import org.apache.commons.logging.LogFactory;
|
27 | 26 |
|
28 |
| -import org.springframework.beans.factory.BeanCreationException; |
29 | 27 | import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
| 28 | +import org.springframework.lang.Nullable; |
30 | 29 | import org.springframework.util.Assert;
|
31 |
| -import org.springframework.util.ReflectionUtils; |
32 |
| -import org.springframework.util.StringUtils; |
33 | 30 |
|
34 | 31 | /**
|
35 | 32 | * An internal class used to track {@link BeanOverrideHandler}-related state after
|
36 |
| - * the bean factory has been processed and to provide field injection utilities |
37 |
| - * for test execution listeners. |
| 33 | + * the bean factory has been processed and to provide lookup facilities to test |
| 34 | + * execution listeners. |
38 | 35 | *
|
39 | 36 | * @author Simon Baslé
|
40 | 37 | * @author Sam Brannen
|
@@ -63,6 +60,7 @@ class BeanOverrideRegistry {
|
63 | 60 | * <p>Also associates a {@linkplain BeanOverrideStrategy#WRAP "wrapping"} handler
|
64 | 61 | * with the given {@code beanName}, allowing for subsequent wrapping of the
|
65 | 62 | * bean via {@link #wrapBeanIfNecessary(Object, String)}.
|
| 63 | + * @see #getBeanForHandler(BeanOverrideHandler, Class) |
66 | 64 | */
|
67 | 65 | void registerBeanOverrideHandler(BeanOverrideHandler handler, String beanName) {
|
68 | 66 | Assert.state(!this.handlerToBeanNameMap.containsKey(handler), () ->
|
@@ -107,23 +105,22 @@ Object wrapBeanIfNecessary(Object bean, String beanName) {
|
107 | 105 | return handler.createOverrideInstance(beanName, null, bean, this.beanFactory);
|
108 | 106 | }
|
109 | 107 |
|
110 |
| - void inject(Object target, BeanOverrideHandler handler) { |
111 |
| - Field field = handler.getField(); |
112 |
| - Assert.notNull(field, () -> "BeanOverrideHandler must have a non-null field: " + handler); |
| 108 | + /** |
| 109 | + * Get the bean instance that was created by the provided {@link BeanOverrideHandler}. |
| 110 | + * @param handler the {@code BeanOverrideHandler} that created the bean |
| 111 | + * @param requiredType the required bean type |
| 112 | + * @return the bean instance, or {@code null} if the provided handler is not |
| 113 | + * registered in this registry |
| 114 | + * @since 6.2.6 |
| 115 | + * @see #registerBeanOverrideHandler(BeanOverrideHandler, String) |
| 116 | + */ |
| 117 | + @Nullable |
| 118 | + Object getBeanForHandler(BeanOverrideHandler handler, Class<?> requiredType) { |
113 | 119 | String beanName = this.handlerToBeanNameMap.get(handler);
|
114 |
| - Assert.state(StringUtils.hasLength(beanName), () -> "No bean found for BeanOverrideHandler: " + handler); |
115 |
| - inject(field, target, beanName); |
116 |
| - } |
117 |
| - |
118 |
| - private void inject(Field field, Object target, String beanName) { |
119 |
| - try { |
120 |
| - Object bean = this.beanFactory.getBean(beanName, field.getType()); |
121 |
| - ReflectionUtils.makeAccessible(field); |
122 |
| - ReflectionUtils.setField(field, target, bean); |
123 |
| - } |
124 |
| - catch (Throwable ex) { |
125 |
| - throw new BeanCreationException("Could not inject field '" + field + "'", ex); |
| 120 | + if (beanName != null) { |
| 121 | + return this.beanFactory.getBean(beanName, requiredType); |
126 | 122 | }
|
| 123 | + return null; |
127 | 124 | }
|
128 | 125 |
|
129 | 126 | }
|
0 commit comments