mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Cache HandlerMethod with resolved bean if singleton
See gh-36278
This commit is contained in:
@@ -91,6 +91,8 @@ public class HandlerMethod extends AnnotatedMethod {
|
||||
|
||||
private @Nullable HandlerMethod resolvedFromHandlerMethod;
|
||||
|
||||
private @Nullable HandlerMethod resolvedBeanHandlerMethod;
|
||||
|
||||
private final String description;
|
||||
|
||||
|
||||
@@ -298,8 +300,10 @@ public class HandlerMethod extends AnnotatedMethod {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the HandlerMethod from which this HandlerMethod instance was
|
||||
* resolved via {@link #createWithResolvedBean()}.
|
||||
* Return the original HandlerMethod instance that from which the current
|
||||
* HandlerMethod instance was created via either
|
||||
* {@link #createWithValidateFlags()} or {@link #createWithResolvedBean()}.
|
||||
* The original instance is needed for cached CORS config lookups.
|
||||
*/
|
||||
public @Nullable HandlerMethod getResolvedFromHandlerMethod() {
|
||||
return this.resolvedFromHandlerMethod;
|
||||
@@ -322,6 +326,10 @@ public class HandlerMethod extends AnnotatedMethod {
|
||||
* <p>If the {@link #getBean() handler} is not String, return the same instance.
|
||||
*/
|
||||
public HandlerMethod createWithResolvedBean() {
|
||||
if (this.resolvedBeanHandlerMethod != null) {
|
||||
return this.resolvedBeanHandlerMethod;
|
||||
}
|
||||
|
||||
if (!(this.bean instanceof String beanName)) {
|
||||
return this;
|
||||
}
|
||||
@@ -329,7 +337,13 @@ public class HandlerMethod extends AnnotatedMethod {
|
||||
Assert.state(this.beanFactory != null, "Cannot resolve bean name without BeanFactory");
|
||||
Object handler = this.beanFactory.getBean(beanName);
|
||||
Assert.notNull(handler, "No handler instance");
|
||||
return new HandlerMethod(this, handler, false);
|
||||
|
||||
HandlerMethod handlerMethod = new HandlerMethod(this, handler, false);
|
||||
if (this.beanFactory.isSingleton(beanName)) {
|
||||
this.resolvedBeanHandlerMethod = handlerMethod;
|
||||
}
|
||||
|
||||
return handlerMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user