简体   繁体   English

Spring aspectj注释切入点

[英]Spring aspectj annotation pointcut

I was trying to create an Aspectj pointcut on method annotation but I failed all the time with different approaches. 我试图在方法注释上创建一个Aspectj切入点,但我总是以不同的方法失败。 I'm using aspectj autoproxy (I have no other weaving configured in my spring context). 我正在使用aspectj autoproxy(我没有在我的spring上下文中配置其他编织)。 My classes look like this: 我的课程看起来像这样:

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

So I'm wondering why the aspectj won't create the pointcut. 所以我想知道为什么aspectj不会创建切入点。 I managed to make it work using execution(* *(..) throws SomeExc) which does the job for me but I still want to know what I did wrong. 我设法让它工作使用execution(* *(..) throws SomeExc)为我做的工作,但我仍然想知道我做错了什么。

Also since method1 is defined in an interface and I specify the annotation on implementing class, is there a way to make it work this way? 此外,由于method1是在接口中定义的,并且我在实现类上指定了注释,有没有办法让它以这种方式工作? Other proxying mechanisms like transaction management/security works this way in other parts of spring right? 其他代理机制,如事务管理/安全性在弹簧的其他部分以这种方式工作吗? And if I'm using interface proxying would specifying the pointcut on implementing class create the pointcut? 如果我使用接口代理将指定实现类的切入点创建切入点? (I guess not since I'm not using cglib) (我猜不是因为我没有使用cglib)

try to add @Component to MyAspect class 尝试将@Component添加到MyAspect类

@Component
@Aspect
public class MyAspect {
...

simply mark your aspect method with 只需使用标记方法

@After("@annotation(package.SomeAnnotation)")

Have a look at this for a step by step guide 看一下这个分步指南

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

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