Skip to content

Commit 8c77711

Browse files
committed
Revise commit #52d068 with corrected test
Update test detecting RouterFunction beans in parent contexts to use different bean names and avoid shadowing. Changed the fix accordingly given that BeanProvider does detect beans in parent contexts. See gh-28595
1 parent d28d603 commit 8c77711

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,19 @@ public void afterPropertiesSet() throws Exception {
154154
* current application context.
155155
*/
156156
private void initRouterFunctions() {
157-
List<RouterFunction<?>> routerFunctions = new ArrayList<>();
158-
detectRouterFunctions(obtainApplicationContext(), routerFunctions);
159-
this.routerFunction = routerFunctions.stream().reduce(RouterFunction::andOther).orElse(null);
160-
logRouterFunctions(routerFunctions);
161-
}
162-
163-
private void detectRouterFunctions(ApplicationContext context, List<RouterFunction<?>> routerFunctions) {
164-
if (this.detectHandlerFunctionsInAncestorContexts && context.getParent() != null) {
165-
detectRouterFunctions(context.getParent(), routerFunctions);
166-
}
167-
context.getBeanProvider(RouterFunction.class)
157+
List<RouterFunction<?>> routerFunctions = obtainApplicationContext()
158+
.getBeanProvider(RouterFunction.class)
168159
.orderedStream()
169160
.map(router -> (RouterFunction<?>) router)
170-
.collect(Collectors.toCollection(() -> routerFunctions));
161+
.collect(Collectors.toList());
162+
163+
ApplicationContext parentContext = obtainApplicationContext().getParent();
164+
if (parentContext != null && !this.detectHandlerFunctionsInAncestorContexts) {
165+
parentContext.getBeanProvider(RouterFunction.class).stream().forEach(routerFunctions::remove);
166+
}
167+
168+
this.routerFunction = routerFunctions.stream().reduce(RouterFunction::andOther).orElse(null);
169+
logRouterFunctions(routerFunctions);
171170
}
172171

173172
private void logRouterFunctions(List<RouterFunction<?>> routerFunctions) {

spring-webmvc/src/test/java/org/springframework/web/servlet/function/support/RouterFunctionMappingTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,16 @@ void detectHandlerFunctionsInAncestorContexts(boolean detect) throws Exception {
9898
HandlerFunction<ServerResponse> function3 = request -> ServerResponse.ok().build();
9999

100100
AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext();
101-
context1.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn1", function1).build());
101+
context1.registerBean("fn1", RouterFunction.class, () -> RouterFunctions.route().GET("/fn1", function1).build());
102102
context1.refresh();
103103

104104
AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext();
105-
context2.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn2", function2).build());
105+
context2.registerBean("fn2", RouterFunction.class, () -> RouterFunctions.route().GET("/fn2", function2).build());
106106
context2.setParent(context1);
107107
context2.refresh();
108108

109109
AnnotationConfigApplicationContext context3 = new AnnotationConfigApplicationContext();
110-
context3.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn3", function3).build());
110+
context3.registerBean("fn3", RouterFunction.class, () -> RouterFunctions.route().GET("/fn3", function3).build());
111111
context3.setParent(context2);
112112
context3.refresh();
113113

0 commit comments

Comments
 (0)