Skip to content

Commit 9b58df8

Browse files
committed
Update HandlerMethod#createWithResolvedBean
Avoid re-creating the instance unless it is a bean name that needs to be resolved through the BeanFactory. Closes gh-34277
1 parent 1cc767e commit 9b58df8

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -317,15 +317,19 @@ public HandlerMethod createWithValidateFlags() {
317317
}
318318

319319
/**
320-
* If the provided instance contains a bean name rather than an object instance,
321-
* the bean name is resolved before a {@link HandlerMethod} is created and returned.
320+
* If the {@link #getBean() handler} is a bean name rather than the actual
321+
* handler instance, resolve the bean name through Spring configuration
322+
* (e.g. for prototype beans), and return a new {@link HandlerMethod}
323+
* instance with the resolved handler.
324+
* <p>If the {@link #getBean() handler} is not String, return the same instance.
322325
*/
323326
public HandlerMethod createWithResolvedBean() {
324-
Object handler = this.bean;
325-
if (this.bean instanceof String beanName) {
326-
Assert.state(this.beanFactory != null, "Cannot resolve bean name without BeanFactory");
327-
handler = this.beanFactory.getBean(beanName);
327+
if (!(this.bean instanceof String beanName)) {
328+
return this;
328329
}
330+
331+
Assert.state(this.beanFactory != null, "Cannot resolve bean name without BeanFactory");
332+
Object handler = this.beanFactory.getBean(beanName);
329333
Assert.notNull(handler, "No handler instance");
330334
return new HandlerMethod(this, handler, false);
331335
}

spring-web/src/test/java/org/springframework/web/method/HandlerMethodTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -72,6 +72,13 @@ void classLevelValidatedAnnotation() {
7272
testValidateReturnValue(target, List.of("getPerson"), false);
7373
}
7474

75+
@Test // gh-34277
76+
void createWithResolvedBeanSameInstance() {
77+
MyClass target = new MyClass();
78+
HandlerMethod handlerMethod = getHandlerMethod(target, "addPerson");
79+
assertThat(handlerMethod.createWithResolvedBean()).isSameAs(handlerMethod);
80+
}
81+
7582
private static void testValidateArgs(Object target, List<String> methodNames, boolean expected) {
7683
for (String methodName : methodNames) {
7784
assertThat(getHandlerMethod(target, methodName).shouldValidateArguments()).isEqualTo(expected);

0 commit comments

Comments
 (0)