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
This commit is contained in:
rstoyanchev 2025-01-23 14:02:27 +00:00
parent 1cc767e90b
commit 9b58df8857
2 changed files with 19 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -317,15 +317,19 @@ public class HandlerMethod extends AnnotatedMethod {
} }
/** /**
* If the provided instance contains a bean name rather than an object instance, * If the {@link #getBean() handler} is a bean name rather than the actual
* the bean name is resolved before a {@link HandlerMethod} is created and returned. * handler instance, resolve the bean name through Spring configuration
* (e.g. for prototype beans), and return a new {@link HandlerMethod}
* instance with the resolved handler.
* <p>If the {@link #getBean() handler} is not String, return the same instance.
*/ */
public HandlerMethod createWithResolvedBean() { public HandlerMethod createWithResolvedBean() {
Object handler = this.bean; if (!(this.bean instanceof String beanName)) {
if (this.bean instanceof String beanName) { return this;
Assert.state(this.beanFactory != null, "Cannot resolve bean name without BeanFactory");
handler = this.beanFactory.getBean(beanName);
} }
Assert.state(this.beanFactory != null, "Cannot resolve bean name without BeanFactory");
Object handler = this.beanFactory.getBean(beanName);
Assert.notNull(handler, "No handler instance"); Assert.notNull(handler, "No handler instance");
return new HandlerMethod(this, handler, false); return new HandlerMethod(this, handler, false);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -72,6 +72,13 @@ class HandlerMethodTests {
testValidateReturnValue(target, List.of("getPerson"), false); testValidateReturnValue(target, List.of("getPerson"), false);
} }
@Test // gh-34277
void createWithResolvedBeanSameInstance() {
MyClass target = new MyClass();
HandlerMethod handlerMethod = getHandlerMethod(target, "addPerson");
assertThat(handlerMethod.createWithResolvedBean()).isSameAs(handlerMethod);
}
private static void testValidateArgs(Object target, List<String> methodNames, boolean expected) { private static void testValidateArgs(Object target, List<String> methodNames, boolean expected) {
for (String methodName : methodNames) { for (String methodName : methodNames) {
assertThat(getHandlerMethod(target, methodName).shouldValidateArguments()).isEqualTo(expected); assertThat(getHandlerMethod(target, methodName).shouldValidateArguments()).isEqualTo(expected);