在我的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");
但后来我没有在控制台中看到我的方法打印输出。我在这里错过了什么?
假设其他一切都是正确的,你也需要@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