简体   繁体   中英

Spring aspectj annotation pointcut

I was trying to create an Aspectj pointcut on method annotation but I failed all the time with different approaches. I'm using aspectj autoproxy (I have no other weaving configured in my spring context). 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. 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.

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? 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)

try to add @Component to MyAspect class

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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