views:

804

answers:

2

I am trying to register an interceptor using a annotation-driven controller configuration. As far as I can tell, I've done everything correctly but when I try testing the interceptor nothing happens. After looking in the logs I found the following:

2010-04-04 20:06:18,231 DEBUG [main] support.AbstractAutowireCapableBeanFactory (AbstractAutowireCapableBeanFactory.java:452) - Finished creating instance of bean     'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0'
2010-04-04 20:06:18,515 DEBUG [main] handler.AbstractDetectingUrlHandlerMapping (AbstractDetectingUrlHandlerMapping.java:86) - Rejected bean name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': no URL paths identified
2010-04-04 20:06:19,109 DEBUG [main] support.AbstractBeanFactory (AbstractBeanFactory.java:241) - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0'

Look at the second line of this log snippet. Is Spring rejecting the DefaultAnnotationHandlerMapping bean? And if so could this be the problem with my interceptor not working?

Here is my application context:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"       
   xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
 default-autowire="byName"> 

 <!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

<!-- Scan for annotations... -->
<context:component-scan base-package="
    com.splash.web.controller, com.splash.web.service, com.splash.web.authentication"/>

<bean id="authorizedUserInterceptor" class="com.splash.web.handler.AuthorizedUserInterceptor"/>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="authorizedUserInterceptor"/>
        </list>
    </property>
</bean>

Here is my interceptor:

package com.splash.web.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

    public class AuthorizedUserInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {

        log.debug(">>> Operation intercepted...");        

        return true;
    }    
}

Does anyone see anything wrong with this? What does the error I mentioned above actually mean and could it have any bearing on the interceptor not being called? Thanks!

+3  A: 

What you're seeing is a clash between <mvc:annotation-driven /> and the explicit bean definition of DefaultAnnotationHandlerMapping.

When you add <mvc:annotation-driven />, Spring declares its own DefaultAnnotationHandlerMapping, and because it appears before your own one, it gets priority. Your interceptor is registered with your DefaultAnnotationHandlerMapping, but that never actually get called.

Try removing <mvc:annotation-driven /> and trying again.

skaffman
Thanks commenting out <mvc:annotation-driven/> worked. So is adding my own DefaultAnnotationHandlerMapping doing the same as this tag does? I guess I need to read up on this a bit more.
richever
@richever: `<mvc:annotation-driven/>` is largely undocumented (an omission that will hopefully be fixed for the 3.0.2 docs), but essentially it registers a bunch of handler mappings and adapters for you, saving you the clutter of doing it yourself in 90% of cases - but not in your case :)
skaffman
A: 

found from http://forum.springsource.org/showthread.php?t=81238. Use

<mvc:annotation-driven/>

followed by

<mvc:interceptors> <bean class="com.splash.web.handler.AuthorizedUserInterceptor"/></mvc:interceptors>
dileeph