用于拦截所有异常的Spring AOP配置 [英] Spring AOP Configuration for Intercepting All Exceptions
问题描述
我正在努力编写/配置一个ThrowsAdvice拦截器,我想拦截整个项目中抛出的所有异常:
I am struggling to write/configure a ThrowsAdvice interceptor that I want to intercept all exceptions thrown throughout my project:
public class ExceptionsInterceptor implements ThrowsAdvice
{
public void afterThrowing(final Method p_oMethod, final Object[] p_oArgArray,
final Object p_oTarget, final Exception p_oException)
{
System.out.println("Exception caught by Spring AOP!");
}
}
我已经成功配置了拦截特定的MethodInterceptor实现我想要分析的方法(看它们执行需要多长时间)。这是我到目前为止的XML配置文件:
I have already successfully configured a MethodInterceptor implementation that intercepts particular methods that I want to profile (see how long it takes them to execute). Here is the XML config file I have so far:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"/>
<bean name="profilingInterceptor" class="org.me.myproject.aop.ProfilingInterceptor"/>
<bean name="exceptionsInterceptor" class="org.me.myproject.aop.ExceptionsInterceptor"/>
<aop:config>
<aop:advisor advice-ref="profilingInterceptor" pointcut="execution(* org.me.myproject.core.Main.doSomething(..))"/>
</aop:config>
My ProfilingInterceptor工作正常且截获正是当我的Main :: doSomething()方法被调用时 - 所以我知道我是ontrack。使用XmlSpy来查看Spring AOP的模式,看起来我可以添加类似下面的内容,以便让我的ExceptionsInterceptor拦截所有抛出的异常:
My ProfilingInterceptor works perfectly and intercepts precisely when my Main::doSomething() method gets invoked - so I known I'm ontrack. Using XmlSpy to look at Spring AOP's schema, it looks like I can add something like the following in order to get my ExceptionsInterceptor to intercept all thrown exceptions:
<aop:aspect>
<after-throwing method=""/>
</aop:aspect>
但是我找不到任何以此为例的文档,我不知道如何配置方法属性,使其成为通配符(*)并匹配所有类和所有方法。
However I cannot find any documentation where this is used as an example, and I have no idea how to configure the method attribute so that its a "wildcard" (*) and matches all classes and all methods.
有人能指出我正确的方向吗?提前致谢!
Can anyone point me in the right direction? Thanks in advance!
推荐答案
根据aspectJ示例,方法参数是指 @AfterThrowing
的建议方法:
According to aspectJ examples method parameter refers to @AfterThrowing
advice method:
@Aspect
public class LoggingAspect {
@AfterThrowing(
pointcut = "execution(* package.addCustomerThrowException(..))",
throwing= "error")
public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
//...
}
}
然后配置:
<aop:after-throwing method="logAfterThrowing" throwing="error" />
希望有所帮助。
这篇关于用于拦截所有异常的Spring AOP配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!