|
25 | 25 | import java.util.List;
|
26 | 26 | import java.util.Map;
|
27 | 27 | import java.util.Set;
|
| 28 | +import java.util.concurrent.atomic.AtomicReference; |
28 | 29 | import java.util.function.Consumer;
|
29 | 30 | import java.util.stream.Stream;
|
30 | 31 |
|
|
51 | 52 | import org.springframework.beans.factory.support.InstanceSupplier;
|
52 | 53 | import org.springframework.beans.factory.support.RegisteredBean;
|
53 | 54 | import org.springframework.beans.factory.support.RootBeanDefinition;
|
| 55 | +import org.springframework.beans.factory.support.SimpleInstantiationStrategy; |
54 | 56 | import org.springframework.core.env.Environment;
|
55 | 57 | import org.springframework.core.io.DefaultResourceLoader;
|
56 | 58 | import org.springframework.core.io.ResourceLoader;
|
@@ -293,6 +295,33 @@ void getNestedWithNoGeneratorUsesReflection(Source source) throws Exception {
|
293 | 295 | assertThat(instance).isEqualTo("1");
|
294 | 296 | }
|
295 | 297 |
|
| 298 | + @Test // gh-33180 |
| 299 | + void getWithNestedInvocationRetainsFactoryMethod() throws Exception { |
| 300 | + AtomicReference<Method> testMethodReference = new AtomicReference<>(); |
| 301 | + AtomicReference<Method> anotherMethodReference = new AtomicReference<>(); |
| 302 | + |
| 303 | + BeanInstanceSupplier<Object> nestedInstanceSupplier = BeanInstanceSupplier |
| 304 | + .forFactoryMethod(AnotherTestStringFactory.class, "another") |
| 305 | + .withGenerator(registeredBean -> { |
| 306 | + anotherMethodReference.set(SimpleInstantiationStrategy.getCurrentlyInvokedFactoryMethod()); |
| 307 | + return "Another"; |
| 308 | + }); |
| 309 | + RegisteredBean nestedRegisteredBean = new Source(String.class, nestedInstanceSupplier).registerBean(this.beanFactory); |
| 310 | + BeanInstanceSupplier<Object> instanceSupplier = BeanInstanceSupplier |
| 311 | + .forFactoryMethod(TestStringFactory.class, "test") |
| 312 | + .withGenerator(registeredBean -> { |
| 313 | + Object nested = nestedInstanceSupplier.get(nestedRegisteredBean); |
| 314 | + testMethodReference.set(SimpleInstantiationStrategy.getCurrentlyInvokedFactoryMethod()); |
| 315 | + return "custom" + nested; |
| 316 | + }); |
| 317 | + RegisteredBean registeredBean = new Source(String.class, instanceSupplier).registerBean(this.beanFactory); |
| 318 | + Object value = instanceSupplier.get(registeredBean); |
| 319 | + |
| 320 | + assertThat(value).isEqualTo("customAnother"); |
| 321 | + assertThat(testMethodReference.get()).isEqualTo(instanceSupplier.getFactoryMethod()); |
| 322 | + assertThat(anotherMethodReference.get()).isEqualTo(nestedInstanceSupplier.getFactoryMethod()); |
| 323 | + } |
| 324 | + |
296 | 325 | @Test
|
297 | 326 | void resolveArgumentsWithNoArgConstructor() {
|
298 | 327 | RootBeanDefinition beanDefinition = new RootBeanDefinition(
|
@@ -1031,4 +1060,18 @@ static class MethodOnInterfaceImpl implements MethodOnInterface {
|
1031 | 1060 |
|
1032 | 1061 | }
|
1033 | 1062 |
|
| 1063 | + static class TestStringFactory { |
| 1064 | + |
| 1065 | + String test() { |
| 1066 | + return "test"; |
| 1067 | + } |
| 1068 | + } |
| 1069 | + |
| 1070 | + static class AnotherTestStringFactory { |
| 1071 | + |
| 1072 | + String another() { |
| 1073 | + return "another"; |
| 1074 | + } |
| 1075 | + } |
| 1076 | + |
1034 | 1077 | }
|
0 commit comments