tags:

views:

138

answers:

1

Hi

I was playing with My First SpringMVC based app..... (3.0.2.RELEASE)

I noticed that AnnotationMethodHandlerAdapter invokes constructor of ServletHandlerMethodResolver, and this constructor invokes init() method of HandlerMethodResolver.

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);
    }

Note: In my case, Proxy.isProxyClass(handlerType) returns false.

This init() method (with help of ReflectionUtils.doWithMethods()) finds @RequestMapping, @InitBinder and @ModelAttribute annotations on the methods of - specified class AND - all of its SuperClasses including Object Class!!!

We should not (and cannot) "put" such annotations on methods of Object class. So, why init() scans the methods of Object class? Please clarify!

Back Link

  • SE
A: 

Try To extend common funcionality and you will see know why.

@Controller
public class IndexController extends CommonFuncionality {

    // impl goes here

}

Where CommonFuncionality contains Spring-MVC stuffs like @InitBinder, @ModelAttribute and so on...

Arthur Ronald F D Garcia
That's write! Support for CommonFuncionality is really a good thing.But, my question is Why Spring MVC looks for annotations on the methods of Object class? When ReflectionUtils.doWithMethods method will be at top in Inheritance Hierarchy, the value of currentHandlerType=java.lang.Object; and I think this is the right point to break the FOR loop. BUT Spring MVC goes ahead and checks that:- Has anybody put @RequestMapping, @InitBinder and @ModelAttribute annotations on methods on Object Class? Which is absolutely redundant check IMHO!
becomputer06
correcting minor typo:- .....annotations on methods *OF* Object Class?.......
becomputer06
@becomputer06 Keep in mind All Java classes *extends* Object. So this code will look **for any parent class** which includes Object (Even when you know it does not contain any Spring related annotation) Just for curiosity: Where can i get Spring source-code ??? I have 2.5 version and it does not contain AnnotationHandlerMapping and AnnotationMethodHandlerAdapter source code. Does it come with Spring 3.0 ???
Arthur Ronald F D Garcia