【问题标题】:Spring AOP AfterThrowing issueSpring AOP AfterThrowing 问题
【发布时间】:2013-09-27 04:57:58
【问题描述】:

请澄清以下内容: 问题是: 为什么先执行@AfterThrowing 建议然后打印异常。 根据定义,我应该首先看到异常,然后是 @AfterThrowing

    public class LoggingAspect {


        @AfterThrowing(value = "execution(public void setName(String))")
        public void afterSetNameAdvice(JoinPoint joinPoint) {
            System.out.println("After:"+joinPoint.toString());
            }

        @Before(value = "execution(public void setName(String))")
        public void beforeSetNameAdvice(JoinPoint joinPoint) {
            System.out.println("Before:"+joinPoint.toString());
            }

SetName Method:

    public void setName(String name) {
        this.name = name;

        throw(new ArithmeticException());

    }

Output:

Before:execution(void com.spring.Employee.setName(String))
After:execution(void com.spring.Employee.setName(String))
Exception in thread "main" java.lang.ArithmeticException
    at com.spring.Employee.setName(Employee.java:41)

编辑帖子并在下面添加答案 我相信这是答案,但我不确定。请有人确认。 在内部,Spring 创建了扩展实际目标类的代理类(您有 Join 点)。 覆盖Join point方法,实现AOP概念。

代理类中的覆盖连接点:

setName(){
致电@Before 建议
调用加入点——这里最初抛出异常。
致电@After 投掷建议
//框架不能首先抛出异常,因为它丢失了在覆盖的 setName() 方法中运行@AfterThrowing Advice 的句柄。所以首先执行@After Throwing Advice 并抛出异常
}

【问题讨论】:

  • 我猜是先投后接。

标签: spring


【解决方案1】:

为什么要先看到异常?基本上AfterThrowing 在方法抛出异常之后。 AfterThrowing 可以处理或修改异常。所以它会在捕获之前出现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2013-05-12
    • 2021-08-18
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多