Skip to content

Commit 36086a8

Browse files
committed
Support bean context for operation method invocation
Signed-off-by: nerdroid <[email protected]>
1 parent a19d4ea commit 36086a8

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

spring-cloud-gateway-server-webmvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/RouterFunctionHolderFactory.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.cloud.gateway.server.mvc.config;
1818

19+
import java.lang.reflect.Modifier;
1920
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.Collections;
@@ -30,12 +31,14 @@
3031
import java.util.function.Consumer;
3132
import java.util.function.Function;
3233

34+
import jakarta.annotation.Nullable;
3335
import org.apache.commons.logging.Log;
3436
import org.apache.commons.logging.LogFactory;
3537

3638
import org.springframework.beans.factory.BeanFactory;
3739
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
3840
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
41+
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
3942
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
4043
import org.springframework.boot.context.properties.bind.Bindable;
4144
import org.springframework.boot.context.properties.bind.Binder;
@@ -351,12 +354,38 @@ private <T> T invokeOperation(OperationMethod operationMethod, Map<String, Objec
351354
else {
352355
args.putAll(operationArgs);
353356
}
354-
ReflectiveOperationInvoker operationInvoker = new ReflectiveOperationInvoker(operationMethod,
355-
this.parameterValueMapper);
357+
358+
ReflectiveOperationInvoker operationInvoker = new ReflectiveOperationInvoker(
359+
resolveInvocationTargetBean(operationMethod), operationMethod, this.parameterValueMapper);
356360
InvocationContext context = new InvocationContext(args, trueNullOperationArgumentResolver);
357361
return operationInvoker.invoke(context);
358362
}
359363

364+
@Nullable
365+
private Object resolveInvocationTargetBean(OperationMethod operationMethod) {
366+
// if the method is static, we don't have to find the invocation target bean
367+
if (Modifier.isStatic(operationMethod.getMethod().getModifiers())) {
368+
return null;
369+
}
370+
371+
try {
372+
if (beanFactory != null) {
373+
return beanFactory.getBean(operationMethod.getMethod().getDeclaringClass());
374+
}
375+
}
376+
catch (NoUniqueBeanDefinitionException e) {
377+
log.warn(LogMessage.format(
378+
"Multiple beans found for type [%s], this non-static operation method [%s] will be invoked without bean context",
379+
operationMethod.getMethod().getDeclaringClass().getName(), operationMethod.getMethod().getName()));
380+
}
381+
catch (NoSuchBeanDefinitionException e) {
382+
log.debug(LogMessage.format(
383+
"No bean registered for type [%s], this non-static operation method [%s] will be invoked without bean context",
384+
operationMethod.getMethod().getDeclaringClass().getName(), operationMethod.getMethod().getName()));
385+
}
386+
return null;
387+
}
388+
360389
private Object bindConfigurable(OperationMethod operationMethod, Map<String, Object> args,
361390
OperationParameter operationParameter) {
362391
Class<?> configurableType = operationParameter.getType();

0 commit comments

Comments
 (0)