简体   繁体   English

在Spring中配置异常处理

[英]Configuring exception handling in spring

Im using extdirectspring and spring in a java webapp. 我在java webapp中使用extdirectspring和spring。

I've recently been trying to set up exception handling basically following this tutorial . 我最近一直在尝试基本上按照本教程设置异常处理。

I have a few context files. 我有一些上下文文件。 But upon adding the following setup to my module which includes the exception handler 但是在将以下设置添加到我的模块(其中包含异常处理程序)后

One called server-context looks like this 一个叫做服务器上下文的样子是这样的

<context:component-scan base-package="blah.eventsModule" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/errorPages/</value>
    </property>
</bean>

<mvc:annotation-driven />

This is the one I'd added the exception handling stuff to 这是我在其中添加异常处理内容的内容

Its supposed to be picking up this class: 它应该接这个课程:

@Component
public class ErrorReporter
{
    @Autowired
    private MyConfig _config;

    @ExceptionHandler(Throwable.class)
    public ModelAndView ShowErrorPage(Exception exception)
    {
        if (_config.debug())
            return new ModelAndView("ShowStackTrace.jsp", "exception", exception);

        return new ModelAndView("Whoops.jsp");
    }
}

I initially left out <mvc:annotation-driven /> to see if it was necessary. 最初,我省略了<mvc:annotation-driven />来查看是否有必要。 A break point in ShowErrorPage wasn't hit so I put it back in. Now I get the following error: 没有找到ShowErrorPage中的断点,因此我将其放回去。现在,我得到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'routerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter ch.ralscha.extdirectspring.controller.RouterController.handlerAdapter; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] is defined: expected single matching bean but found 2:

That's happening in extdirectspring (a third party library). 这是在extdirectspring(第三方库)中发生的。

I've not created RequestMappingHandlerAdapter explicitely but obviously now I've got two of them. 我没有显式创建RequestMappingHandlerAdapter但是显然现在我有两个。 I don't care what extdirect is trying to do with this. 我不在乎extdirect试图做什么。 So if there is a way of scoping what I'm doing then that would be good. 因此,如果有一种方法可以确定我在做什么,那将是很好的。

The only other thing I can think is that spring doesn't like there to be more than one 我能想到的唯一一件事是春天不喜欢有多个

<mvc:annotation-driven />

declaration in a project. 在项目中声明。

Here are some bits of code that look suspicious: 以下是一些看起来可疑的代码:

public class LoggingHandlerExceptionResolver implements HandlerExceptionResolver, Ordered
{
    private final static Logger logger = LoggerFactory.getLogger(LoggingHandlerExceptionResolver.class);

    @Autowired
    private ErrorLogService errorLogService;

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
    ...

    // Returning null allows other HandlerExceptionResolvers to be called (e.g. to show an error page)
    return null;
}

The comment there isn't mine and I don't know whether it is true or not but it looks like its in the same ballpark and suggests that having multiple of these things is ok. 那里的评论不是我的,我也不知道它是否正确,但看起来像是在同一个球场上,并建议将这些事情中的多个都可以。

That class is in our 'core' module 该课程在我们的“核心”模块中

As is this xml snippet: 就像这个xml片段一样:

<!-- Spring MVC configuration -->
<mvc:annotation-driven conversion-service="conversionService">

    <mvc:argument-resolvers>
        <!-- Resolve Spring Data pageable arguments -->
        <bean class="org.springframework.data.web.PageableArgumentResolver" />
    </mvc:argument-resolvers>

    <mvc:message-converters>
        <!-- Wire the custom object mapper into Spring MVC -->
        <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="objectMapper" />
        </bean>
    </mvc:message-converters>

</mvc:annotation-driven>

That doesn't seem to have anything to do with my stuff but it's the only other place mvc:annotation-driven crops up in the project. 这似乎与我的工作无关,但这是mvc:注释驱动的项目中唯一出现的其他地方。

Could anyone shed some light on whats going on? 任何人都可以对发生的事情有所了解吗?

I want to have the two seperate errorhandling methods separate - the new one I'm adding is for a new part of the project - we have 2 deployments - one which is thoroughly broken and is relying on several different components supressing errors. 我想将两种单独的错误处理方法分开-我要添加的新方法用于项目的新部分-我们有2种部署-一种已经彻底破坏,并且依赖于抑制错误的几个不同组件。 In the new one we aren't pushing exceptions under the rug. 在新版本中,我们不会在地毯下推开例外。 This is a bit of a follow up from another question I asked recently about getting sensible exception handling in a webapp . 这是我最近问的另一个问题的跟进,该问题是有关在Webapp中获取明智的异常处理的

We've got 2 separate application-contexts and the flaky application doesnt include the new file at the top. 我们有2个单独的应用程序上下文,而不稳定的应用程序在顶部不包含新文件。 They are both using the core module though. 他们都使用核心模块。

Update 更新资料

If I hack the annotation driven snippet out of the core module then I no longer have the autowired issue but the new exception handling still doesn't get run. 如果我将注释驱动的代码片段从核心模块中删除,那么我将不再遇到自动装配的问题,但是仍然无法运行新的异常处理。

It seems like once I figure out how to get that working then the problem will be how to merge 2 mvc configs? 好像一旦我弄清楚了如何使它工作,那么问题将是如何合并2个mvc配置?

Typical solution to a spring problem: I dont understand what happened but after copying something else I dont understand I now have something which works! 春季问题的典型解决方案:我不了解发生了什么,但是在复制其他内容后,我不了解我现在有一些有效的功能!

I'm not quite sure exactly when I stopped being a programmer and instead became someone who fiddles with config files. 我不太确定我何时停止成为程序员,而是成为摆弄配置文件的人。 It's been a gradual death! 这是一个逐渐的死亡!

I have changed my error handler to follow the style of the other working one. 我更改了错误处理程序,以遵循其他工作方式的样式。

I'm now defining it as a bean in the application context 我现在将其定义为应用程序上下文中的bean

<bean class="blah.LoggingHandlerExceptionResolver"/>
<bean class="blah.ErrorReporter"/>

And it looks like this: 它看起来像这样:

public class ErrorReporter implements HandlerExceptionResolver, Ordered
{
    @Autowired
    private MyConfig _config;

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception exception)
    {
        if (_config.debug())
            return new ModelAndView("ShowStackTrace.jsp", "exception", exception);

        return new ModelAndView("Whoops.jsp");
    }

    @Override
    public int getOrder()
    {
        return 0;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM