Skip to content

Commit c6b20c0

Browse files
committed
Merge branch '6.1.x'
2 parents ee6402e + b5a86de commit c6b20c0

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,13 @@ private T invokeBeanSupplier(Executable executable, ThrowingSupplier<T> beanSupp
213213
if (!(executable instanceof Method method)) {
214214
return beanSupplier.get();
215215
}
216+
Method priorInvokedFactoryMethod = SimpleInstantiationStrategy.getCurrentlyInvokedFactoryMethod();
216217
try {
217218
SimpleInstantiationStrategy.setCurrentlyInvokedFactoryMethod(method);
218219
return beanSupplier.get();
219220
}
220221
finally {
221-
SimpleInstantiationStrategy.setCurrentlyInvokedFactoryMethod(null);
222+
SimpleInstantiationStrategy.setCurrentlyInvokedFactoryMethod(priorInvokedFactoryMethod);
222223
}
223224
}
224225

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

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -55,12 +55,18 @@ public static Method getCurrentlyInvokedFactoryMethod() {
5555
}
5656

5757
/**
58-
* Set the factory method currently being invoked or {@code null} to reset.
58+
* Set the factory method currently being invoked or {@code null} to remove
59+
* the current value, if any.
5960
* @param method the factory method currently being invoked or {@code null}
6061
* @since 6.0
6162
*/
6263
public static void setCurrentlyInvokedFactoryMethod(@Nullable Method method) {
63-
currentlyInvokedFactoryMethod.set(method);
64+
if (method != null) {
65+
currentlyInvokedFactoryMethod.set(method);
66+
}
67+
else {
68+
currentlyInvokedFactoryMethod.remove();
69+
}
6470
}
6571

6672

@@ -134,22 +140,17 @@ public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, Bean
134140
try {
135141
ReflectionUtils.makeAccessible(factoryMethod);
136142

137-
Method priorInvokedFactoryMethod = currentlyInvokedFactoryMethod.get();
143+
Method priorInvokedFactoryMethod = getCurrentlyInvokedFactoryMethod();
138144
try {
139-
currentlyInvokedFactoryMethod.set(factoryMethod);
145+
setCurrentlyInvokedFactoryMethod(factoryMethod);
140146
Object result = factoryMethod.invoke(factoryBean, args);
141147
if (result == null) {
142148
result = new NullBean();
143149
}
144150
return result;
145151
}
146152
finally {
147-
if (priorInvokedFactoryMethod != null) {
148-
currentlyInvokedFactoryMethod.set(priorInvokedFactoryMethod);
149-
}
150-
else {
151-
currentlyInvokedFactoryMethod.remove();
152-
}
153+
setCurrentlyInvokedFactoryMethod(priorInvokedFactoryMethod);
153154
}
154155
}
155156
catch (IllegalArgumentException ex) {

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanInstanceSupplierTests.java

+43
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.Map;
2727
import java.util.Set;
28+
import java.util.concurrent.atomic.AtomicReference;
2829
import java.util.function.Consumer;
2930
import java.util.stream.Stream;
3031

@@ -51,6 +52,7 @@
5152
import org.springframework.beans.factory.support.InstanceSupplier;
5253
import org.springframework.beans.factory.support.RegisteredBean;
5354
import org.springframework.beans.factory.support.RootBeanDefinition;
55+
import org.springframework.beans.factory.support.SimpleInstantiationStrategy;
5456
import org.springframework.core.env.Environment;
5557
import org.springframework.core.io.DefaultResourceLoader;
5658
import org.springframework.core.io.ResourceLoader;
@@ -293,6 +295,33 @@ void getNestedWithNoGeneratorUsesReflection(Source source) throws Exception {
293295
assertThat(instance).isEqualTo("1");
294296
}
295297

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+
296325
@Test
297326
void resolveArgumentsWithNoArgConstructor() {
298327
RootBeanDefinition beanDefinition = new RootBeanDefinition(
@@ -1031,4 +1060,18 @@ static class MethodOnInterfaceImpl implements MethodOnInterface {
10311060

10321061
}
10331062

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+
10341077
}

0 commit comments

Comments
 (0)