Spring mvc 为什么(Spring 3)HandlerMethodResolverinit()方法在对象类的方法上查找注释?
我在玩我的第一个基于SpringMVC的应用程序。。。。。3.0.2.1发布 我注意到AnnotationMethodHandlerAdapter调用ServletHandlerMethodResolver的构造函数,该构造函数调用HandlerMethodResolver的init方法Spring mvc 为什么(Spring 3)HandlerMethodResolverinit()方法在对象类的方法上查找注释?,spring-mvc,Spring Mvc,我在玩我的第一个基于SpringMVC的应用程序。。。。。3.0.2.1发布 我注意到AnnotationMethodHandlerAdapter调用ServletHandlerMethodResolver的构造函数,该构造函数调用HandlerMethodResolver的init方法 public void init(Class<?> handlerType) { Class<?>[] handlerTypes = Proxy.isPr
public void init(Class<?> handlerType) {
Class<?>[] handlerTypes =
Proxy.isProxyClass(handlerType) ? handlerType.getInterfaces() : new Class<?>[] {handlerType};
for (final Class<?> currentHandlerType : handlerTypes) {
ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() {
public void doWith(Method method) {
Method specificMethod = ClassUtils.getMostSpecificMethod(method, currentHandlerType);
if (isHandlerMethod(method)) {
handlerMethods.add(specificMethod);
}
else if (method.isAnnotationPresent(InitBinder.class)) {
initBinderMethods.add(specificMethod);
}
else if (method.isAnnotationPresent(ModelAttribute.class)) {
modelAttributeMethods.add(specificMethod);
}
}
}, ReflectionUtils.NON_BRIDGED_METHODS);
}
注意:在我的例子中,Proxy.isProxyClasshandlerType返回false
此init方法在ReflectionUtils.doWithMethods的帮助下在的方法上查找@RequestMapping、@InitBinder和@ModelAttribute注释
-指定类别及
-它的所有超类,包括对象类
我们不应该也不能将这样的注释放在对象类的方法上。那么,为什么init扫描对象类的方法呢?请澄清
东南方
尝试扩展公共函数性,你就会知道为什么
@Controller
public class IndexController extends CommonFuncionality {
// impl goes here
}
其中CommonFunctionality包含Spring MVC内容,如@InitBinder、@ModelAttribute等…这就是write!支持公共功能确实是一件好事。但是,我的问题是为什么SpringMVC会在对象类的方法上寻找注释?当ReflectionUtils.doWithMethods方法位于继承层次结构的顶部时,currentHandlerType的值=java.lang.Object;我认为这是打破FOR循环的正确点。但SpringMVC继续检查:-是否有人在对象类的方法上放置了@RequestMapping、@InitBinder和@ModelAttribute注释?这绝对是多余的检查@becomputer06请记住所有Java类都扩展了对象。所以,这段代码将查找任何包含对象的父类,即使您知道它不包含任何与Spring相关的注释,这只是出于好奇:我在哪里可以获得Spring源代码???我有2.5版本,它不包含AnnotationHandlerMapping和AnnotationMethodHandlerAdapter源代码。它是否与Spring3.0一起提供???