diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java index 9bce118e9e..4c05f9ccb0 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java @@ -154,20 +154,19 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini * current application context. */ private void initRouterFunctions() { - List> routerFunctions = new ArrayList<>(); - detectRouterFunctions(obtainApplicationContext(), routerFunctions); - this.routerFunction = routerFunctions.stream().reduce(RouterFunction::andOther).orElse(null); - logRouterFunctions(routerFunctions); - } - - private void detectRouterFunctions(ApplicationContext context, List> routerFunctions) { - if (this.detectHandlerFunctionsInAncestorContexts && context.getParent() != null) { - detectRouterFunctions(context.getParent(), routerFunctions); - } - context.getBeanProvider(RouterFunction.class) + List> routerFunctions = obtainApplicationContext() + .getBeanProvider(RouterFunction.class) .orderedStream() .map(router -> (RouterFunction) router) - .collect(Collectors.toCollection(() -> routerFunctions)); + .collect(Collectors.toList()); + + ApplicationContext parentContext = obtainApplicationContext().getParent(); + if (parentContext != null && !this.detectHandlerFunctionsInAncestorContexts) { + parentContext.getBeanProvider(RouterFunction.class).stream().forEach(routerFunctions::remove); + } + + this.routerFunction = routerFunctions.stream().reduce(RouterFunction::andOther).orElse(null); + logRouterFunctions(routerFunctions); } private void logRouterFunctions(List> routerFunctions) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/support/RouterFunctionMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/support/RouterFunctionMappingTests.java index 8b8ff09eec..8d28f5e666 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/support/RouterFunctionMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/support/RouterFunctionMappingTests.java @@ -98,16 +98,16 @@ class RouterFunctionMappingTests { HandlerFunction function3 = request -> ServerResponse.ok().build(); AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext(); - context1.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn1", function1).build()); + context1.registerBean("fn1", RouterFunction.class, () -> RouterFunctions.route().GET("/fn1", function1).build()); context1.refresh(); AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext(); - context2.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn2", function2).build()); + context2.registerBean("fn2", RouterFunction.class, () -> RouterFunctions.route().GET("/fn2", function2).build()); context2.setParent(context1); context2.refresh(); AnnotationConfigApplicationContext context3 = new AnnotationConfigApplicationContext(); - context3.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn3", function3).build()); + context3.registerBean("fn3", RouterFunction.class, () -> RouterFunctions.route().GET("/fn3", function3).build()); context3.setParent(context2); context3.refresh();