Java 带有Spring@ControllerAdvice的全局错误处理程序不';行不通

Java 带有Spring@ControllerAdvice的全局错误处理程序不';行不通,java,spring,error-handling,spring-3,Java,Spring,Error Handling,Spring 3,有人知道为什么下面的代码无法捕获异常吗 package org.rythmengine.spring.web.servlet.view; import org.rythmengine.RythmEngine; import org.rythmengine.exception.RythmException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.b

有人知道为什么下面的代码无法捕获异常吗

package org.rythmengine.spring.web.servlet.view;

import org.rythmengine.RythmEngine;
import org.rythmengine.exception.RythmException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class RythmExceptionHandler {

    RythmEngine engine;

    @Autowired
    public RythmExceptionHandler(RythmConfigurer conf) {
        this.engine = conf.getRythmEngine();
    }

    @ExceptionHandler(value = RythmException.class)
    public ModelAndView defaultErrorHandler(RythmException e) throws Exception {
        if (engine.mode().isProd()) {
            throw e;
        }
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.setViewName("errors/500.html");
        return mav;
    }

}

发现了问题。需要在配置文件中添加一行:

<context:component-scan base-package="org.rythmengine.spring.web.servlet.view"/>
到目前为止,这还不是结束。它成功捕获了用户控制器代码中的异常,但没有捕获视图渲染过程中的异常。我正在寻找一种添加
HandlerInterceptor
的方法,以便它能够处理
DispatcherServlet.triggerAfterCompletion(…)中的错误

更新

上述代码被证明不起作用。最终的解决方案是向任意类添加以下注释:

@Configuration
@ComponentScan("org.rythmengine.spring.web.servlet.view")
是的,现在我不需要用户将
添加到他们的xml配置文件中

关于渲染时异常处理,我在
RythmView
类中内部缓存异常,以防在
checkResource(Locale)
调用中出现错误(通常是编译错误或解析错误),在下面的
renderMergedTemplateModel
调用中,我将检查是否存在缓存的异常,如果存在异常,则渲染异常屏幕,如下所示:

是的,只有将
RythmConfigurer
的devMode设置为true(默认为false)时,开发人员友好的屏幕功能才可用:

<bean id="rythmConfig" class="org.rythmengine.spring.web.servlet.view.RythmConfigurer">
    <property name="resourceLoaderPath" value="/WEB-INF/rythm/"/>
    <property name="outputRequestParameters" value="false"/>
    <property name="devMode" value="true"/>
</bean>


发布您的配置。哪个例外?堆栈跟踪在哪里?代码在哪里抛出它?看起来HandlerInterceptor无法真正处理异常。我刚刚发现代码以编程方式扫描在allOkey上不起作用,最后我向另一个类添加了两个注释。请参阅答案中的更新在我的案例中@EnableWebMvc就足够了
<bean id="rythmConfig" class="org.rythmengine.spring.web.servlet.view.RythmConfigurer">
    <property name="resourceLoaderPath" value="/WEB-INF/rythm/"/>
    <property name="outputRequestParameters" value="false"/>
    <property name="devMode" value="true"/>
</bean>