为什么我的方面代码在抛出异常时不会运行?

问题描述 投票:1回答:1

在我的spring项目中,我添加了两个依赖项:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.9.2</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.2</version>
</dependency>

然后我创建了一个类:

package com.my.company.package.handling;

@Aspect
public class MyAspect {
@AfterThrowing(pointcut = "execution(* com.my.company.package.*(..))", throwing = "ex")
    public void logAfterThrowing(Exception ex) {
        System.out.println("exception "+ex.getLocalizedMessage())
    }
}

现在在其他一些类(存储在包中:com.my.company.package.someOtherPackage)我抛出异常:

throw new IOException("here comes error");

但后来我没有在控制台中看到我的方法打印输出。我在这里错过了什么?

java spring aspectj
1个回答
0
投票

假设其他一切都是正确的,你也需要@Component注释,你还需要在任何类的执行字符串中使用另一个*。

@Aspect
@Component
public class MyAspect {
@AfterThrowing(pointcut = "execution(* com.my.company.package.*.*(..))", throwing = "ex")
    public void logAfterThrowing(Exception ex) {
        System.out.println("exception "+ex.getLocalizedMessage())
    }
}

这是一个有效的example

© www.soinside.com 2019 - 2024. All rights reserved.