在我的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");
但是我在控制台中看不到我的Aspect方法的打印输出。我在这里想念什么?
答案 0 :(得分: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