Browse Source

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
pull/28653/head
rstoyanchev 2 years ago
parent
commit
8c777111fa
  1. 23
      spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java
  2. 6
      spring-webmvc/src/test/java/org/springframework/web/servlet/function/support/RouterFunctionMappingTests.java

23
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. * current application context.
*/ */
private void initRouterFunctions() { private void initRouterFunctions() {
List<RouterFunction<?>> routerFunctions = new ArrayList<>(); List<RouterFunction<?>> routerFunctions = obtainApplicationContext()
detectRouterFunctions(obtainApplicationContext(), routerFunctions); .getBeanProvider(RouterFunction.class)
this.routerFunction = routerFunctions.stream().reduce(RouterFunction::andOther).orElse(null);
logRouterFunctions(routerFunctions);
}
private void detectRouterFunctions(ApplicationContext context, List<RouterFunction<?>> routerFunctions) {
if (this.detectHandlerFunctionsInAncestorContexts && context.getParent() != null) {
detectRouterFunctions(context.getParent(), routerFunctions);
}
context.getBeanProvider(RouterFunction.class)
.orderedStream() .orderedStream()
.map(router -> (RouterFunction<?>) router) .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<RouterFunction<?>> routerFunctions) { private void logRouterFunctions(List<RouterFunction<?>> routerFunctions) {

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

@ -98,16 +98,16 @@ class RouterFunctionMappingTests {
HandlerFunction<ServerResponse> function3 = request -> ServerResponse.ok().build(); HandlerFunction<ServerResponse> function3 = request -> ServerResponse.ok().build();
AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext(); 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(); context1.refresh();
AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext(); 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.setParent(context1);
context2.refresh(); context2.refresh();
AnnotationConfigApplicationContext context3 = new AnnotationConfigApplicationContext(); 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.setParent(context2);
context3.refresh(); context3.refresh();

Loading…
Cancel
Save