4
votes

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)

2
Try to use full package path define your aop annotation expressing. - Jason
Yep I tried that if you mean using @annotation(full.class.path.SomeAnnotation) in my pointcut definition. But thanks anyway ;-) - Honza R

2 Answers

3
votes

try to add @Component to MyAspect class

@Component
@Aspect
public class MyAspect {
...
3
votes

simply mark your aspect method with

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

Have a look at this for a step by step guide