Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ControllerAdvice Not Firing

I'm working on a Spring MVC/Webflow Application (version 3.2) and trying to get exception handling working where I can output a custom exception message to a logfile and error.jsp. The problem I'm having is that the Exception Handler is not getting fired. I've created the following class and annotated it "@ControllerAdvice" and put it into the same package as my controller that is throwing the exception:

@ControllerAdvice
public class MyCustomExceptionController {

    @ExceptionHandler(MyCustomException.class)
    public ModelAndView handleMyException(MyCustomException ex) {   
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/error/error");
        modelAndView.addObject("errorId", ex.getErrorId());
        modelAndView.addObject("message", ex.getErrorMessage());        
        return modelAndView;
    }
}

and added the following to the mvc-config File:

<mvc:annotation-driven/>

And included the following in my app-config File:

<context:component-scan base-package="package containing my controllers and MyCustomExceptionController">
        <context:include-filter type="annotation" 
    expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan> 

Any ideas why this isn't working?

like image 767
user676567 Avatar asked Feb 19 '14 15:02

user676567


1 Answers

The <mvc:annotation-driven/> element implicitly registers a ExceptionHandlerExceptionResolver bean. This class has a initExceptionHandlerAdviceCache() method which scans beans in the context to find those whose class type is annotated with @ControllerAdvice.

It does this by first calling ControllerAdviceBean.findAnnotatedBeans(ApplicationContext). Internally, this method uses ApplicationContext#getBeanDefinitionNames(). The javadoc of this method states

Does not consider any hierarchy this factory may participate

To clarify what this means. When you declare a ContextLoaderListener in your deployment descriptor, it loads what we call a root or application ApplicationContext and makes it available in the ServletContext. When you then declare a DispatcherServlet, it creates its own servlet ApplicationContext and uses any ApplicationContext it finds in the ServletContext attributes loaded by the ContextLoaderListener as a parent to that context. The hierarchy looks like so

Root ApplicationContext // loaded by the ContextLoaderListener
            |
Servlet ApplicationContext // loaded by the DispatcherServlet

Every ApplicationContext has access to beans in parent contexts, but not the other way around.

The method above chooses not to use the beans in parent contexts and so only has access to beans in the current ApplicationContext (BeanFactory really).

As such, if your

<context:component-scan .../>

is declared in a root ApplicationContext as I'll assume from the name app-config, but the

<mvc:annotation-driven />

is declared in the servlet ApplicationContext, again assuming from mvc-config, then the ExceptionHandlerExceptionResolver looking for @ControllerAdvice beans will not find any. It is looking for beans in the servlet context but they aren't there, they are in the root context.

like image 185
Sotirios Delimanolis Avatar answered Oct 13 '22 00:10

Sotirios Delimanolis