Spring5升级:如何替换已废弃的DefaultAnnotationHandlerMapping
二、替换已移除的HandlerMethodResolver
HandlerMethodResolver已从Spring中移除,可使用MethodIntrospector工具类替代,用于解析handler类中的请求映射方法:
// 替换原HandlerMethodResolver的初始化逻辑 Set<Method> requestMappingMethods = MethodIntrospector.selectMethods(handlerType, (MethodIntrospector.MetadataLookup<RequestMapping>) method -> AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class)); // 若需要封装为HandlerMethod(包含方法所属Bean实例等元数据) List<HandlerMethod> handlerMethods = new ArrayList<>(); Object handlerInstance = applicationContext.getBean(handlerType); for (Method method : requestMappingMethods) { handlerMethods.add(new HandlerMethod(handlerInstance, method)); }
关键说明:
MethodIntrospector.selectMethods可精准筛选出带有@RequestMapping注解的方法,AnnotatedElementUtils.findMergedAnnotation能处理注解继承、元注解合并等场景。- 若需要原
HandlerMethodResolver的方法管理能力,可将筛选出的方法封装为HandlerMethod,它包含了方法所属Bean实例、方法本身等完整元数据,满足大部分业务需求。
内容的提问来源于stack exchange,提问作者RayCoding
相关产品推荐
相关产品推荐

