简体   繁体   English

异常处理弹簧

[英]Exception Handling Spring

We are converting our app from Struts 1.2 to Springs 3.0. 我们正在将我们的应用程序从Struts 1.2转换为Springs 3.0。 So we are making changes in exception handling done in our Struts 2 code. 因此,我们正在更改在Struts 2代码中完成的异常处理。

As per my undestanding this is how we need to do. 按照我的理解,这就是我们需要做的。

  1. @ExceptionHandler can be used only in the Controller layer of Springs. @ExceptionHandler只能在Springs的Controller层中使用。
  2. We can use AOP for handling Unchecked Exceptions. 我们可以使用AOP处理未检查的异常。
  3. Checked Exceptions in Struts will remain as it is and can be converted to unchecked exceptions Struts中的检查异常将保持原样,并且可以转换为非检查异常

Now I have following questions. 现在我有以下问题。

  1. Whats the benefit of using AOP for handling unchecked exceptions except that the unchecked exception need not be handelled in my code? 使用AOP处理未经检查的异常有什么好处,除了无需在我的代码中处理未经检查的异常之外?
  2. Whats the benefit of converting checked exception to Unchecked exception? 将已检查的异常转换为未检查的异常有什么好处?
  3. Should I go ahead and convert my checked exceptions in struts to unchecked exception and us the AOP? 我应该继续将Struts中的检查异常转换为非检查异常,然后将我们转换为AOP吗?
  4. What could be the better approach to handle Exceptions in Spring 3.0 ? 在Spring 3.0中处理异常的更好方法是什么?

Have a look at Spring's facilities for exception handling: 看一下Spring用于异常处理的工具:

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-exceptionhandlers http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-exceptionhandlers

The idea is similar to your approach for using AOP, but requires a lot less configuration. 这个想法类似于您使用AOP的方法,但是所需的配置要少得多。 Essentially, you register a handler for any kind of unchecked exception your code throws. 本质上,您为代码引发的任何未经检查的异常注册处理程序。 This lets you test your code by asserting whether or not the exception was thrown, and decoupling that logic from handling and recovering from the exception. 这使您可以通过断言是否引发异常来测试代码,并将该逻辑与处理异常以及从异常中恢复脱钩。

The @ExceptionHandler value can be set to an array of Exception types. 可以将@ExceptionHandler值设置为Exception类型的数组。 If an exception is thrown matches one of the types in the list, then the method annotated with the matching @ExceptionHandler will be invoked. 如果抛出的异常与列表中的一种类型匹配,则将调用以匹配的@ExceptionHandler注释的方法。 If the annotation value is not set then the exception types listed as method arguments are used. 如果未设置注释值,则使用列为方法参数的异常类型。

Spring uses AOP internally which you need not work on. Spring内部使用AOP,您无需进行任何操作。 It gives you all the bolt and nuts code. 它为您提供了所有螺栓和螺母代码。

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

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