Close

Spring MVC - BeanFactoryUtils Examples

[Last Updated: Nov 8, 2025]

Spring MVC 

Finding all HandlerMapping registered with Spring context.

@Controller
public class TestController {

@Autowired
ApplicationContext context;

@RequestMapping(value = "/test")
@ResponseBody
public String handleRequest() {
Map<String, HandlerMapping> matchingBeans =
BeanFactoryUtils.beansOfTypeIncludingAncestors(
context,
HandlerMapping.class,
true,
false);

matchingBeans
.entrySet()
.stream()
.sorted(Comparator.comparingInt((Map.Entry<String, HandlerMapping> e) ->
((Ordered) e.getValue()).getOrder())
.thenComparing(Map.Entry::getKey))
.forEach(e -> System.out.printf("order:%s %s=%s%n",
((Ordered) e.getValue()).getOrder(),
e.getKey(),
e.getValue().getClass()
.getSimpleName()));
return "response from /test";
}
}
Original Post




Finding all HandlerAdapter registered with Spring context.

@Controller
public class TestController {

@Autowired
ApplicationContext context;

@RequestMapping(value = "/test")
@ResponseBody
public String handleRequest() {
BeanFactoryUtils
.beansOfTypeIncludingAncestors(context,
HandlerAdapter.class,
true,
false)
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.forEach(e ->
System.out.printf("%s=%s%n",
e.getKey(),
e.getValue().getClass()
.getSimpleName()));
return "response from /test";
}

}
Original Post




See Also

Join