Browse Source

Cache HandlerMethod with resolved bean if singleton

See gh-36278
pull/36465/head
rstoyanchev 1 month ago
parent
commit
6162d89042
  1. 20
      spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java

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

@ -96,6 +96,8 @@ public class HandlerMethod extends AnnotatedMethod {
@Nullable @Nullable
private HandlerMethod resolvedFromHandlerMethod; private HandlerMethod resolvedFromHandlerMethod;
private @Nullable HandlerMethod resolvedBeanHandlerMethod;
private final String description; private final String description;
@ -305,8 +307,10 @@ public class HandlerMethod extends AnnotatedMethod {
} }
/** /**
* Return the HandlerMethod from which this HandlerMethod instance was * Return the original HandlerMethod instance that from which the current
* resolved via {@link #createWithResolvedBean()}. * HandlerMethod instance was created via either
* {@link #createWithValidateFlags()} or {@link #createWithResolvedBean()}.
* The original instance is needed for cached CORS config lookups.
*/ */
@Nullable @Nullable
public HandlerMethod getResolvedFromHandlerMethod() { public HandlerMethod getResolvedFromHandlerMethod() {
@ -330,6 +334,10 @@ public class HandlerMethod extends AnnotatedMethod {
* <p>If the {@link #getBean() handler} is not String, return the same instance. * <p>If the {@link #getBean() handler} is not String, return the same instance.
*/ */
public HandlerMethod createWithResolvedBean() { public HandlerMethod createWithResolvedBean() {
if (this.resolvedBeanHandlerMethod != null) {
return this.resolvedBeanHandlerMethod;
}
if (!(this.bean instanceof String beanName)) { if (!(this.bean instanceof String beanName)) {
return this; return this;
} }
@ -337,7 +345,13 @@ public class HandlerMethod extends AnnotatedMethod {
Assert.state(this.beanFactory != null, "Cannot resolve bean name without BeanFactory"); Assert.state(this.beanFactory != null, "Cannot resolve bean name without BeanFactory");
Object handler = this.beanFactory.getBean(beanName); Object handler = this.beanFactory.getBean(beanName);
Assert.notNull(handler, "No handler instance"); 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;
} }
/** /**

Loading…
Cancel
Save