Spring aspectj注释切入点

时间:2013-02-19 08:34:16

标签: java spring aop

我试图在方法注释上创建一个Aspectj切入点,但我总是以不同的方法失败。我正在使用aspectj autoproxy(我没有在我的spring上下文中配置其他编织)。我的课程看起来像这样:

public interface Intf
{
  @SomeAnnotation
  void method1() throws SomeExc;
}

public class Impl implements Intf
{
  @Override
  public void method1() throws SomeExc
  {
    //...
  }
}

@Aspect
public class MyAspect
{
  @AfterThrowing(
    pointcut = "execution(* *(..)) && @annotation(SomeAnnotation)",
    throwing = "error")
  public void afterThrowing(JoinPoint jp, Throwable error)
  {
    System.err.println(error.getMessage());
  }
}

@Component
public class Usage
{
  @Autowired
  Intf intf;

  public void doStuff()
  {
    intf.method1();
  }
}

所以我想知道为什么aspectj不会创建切入点。我设法使用execution(* *(..) throws SomeExc)为我工作,但我仍然想知道我做错了什么。

此外,由于method1在接口中定义,并且我在实现类上指定了注释,有没有办法让它以这种方式工作?其他代理机制,如事务管理/安全性在弹簧的其他部分以这种方式工作吗?如果我使用接口代理将指定实现类的切入点创建切入点? (我猜不是因为我没有使用cglib)

2 个答案:

答案 0 :(得分:3)

尝试将@Component添加到MyAspect类

@Component
@Aspect
public class MyAspect {
...

答案 1 :(得分:2)

只需使用

标记方面方法即可
@After("@annotation(package.SomeAnnotation)")

查看this分步指南

相关问题