简体   繁体   English

为什么抛出异常时我的方面代码无法运行?

[英]why does my aspect code not run when exception is thrown?

In my spring project I've added two dependencies: 在我的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>

and then I've created a class: 然后我创建了一个类:

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())
    }
}

Now in some other class (stored in package: com.my.company.package.someOtherPackage ) I'm throwing exception: 现在在其他一些类(存储在com.my.company.package.someOtherPackage包中)中,我抛出了异常:

throw new IOException("here comes error");

but then I don't see the printout from my aspect method in the console. 但是然后我在控制台中看不到我的Aspect方法的打印输出。 What am I missing here? 我在这里想念什么?

Assuming everything else is correct you need a @Component annotation also, you also need another * in the execution string for any class. 假设其他所有内容都正确,则还需要@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())
    }
}

Here's a working example 这是一个有效的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM