Reason remind fro @ControllerAdvice @ExceptiionHandler does not work

Configured following lines in web.xml for servlet:               

                <init-param>

<param-name>detectAllHandlerExceptionResolvers</param-name>

<param-value>false</param-value>

</init-param>

 

===================================explain for the exception handler ====================

The <mvc:annotation-driven/> element implicitly registers a ExceptionHandlerExceptionResolverbean. 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值