Spring pointcut pointcut pointcut

I tried to create pointcut point Aspectj for method annotation but I didn't work with different approaches all the time. I am using aspectj autoproxy (I have no other weave 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 aspectj won't create the pointcut. I managed to get it to work using execution(* *(..) throws SomeExc)

which does the job for me, but I still want to know what I did wrong.

Also, since it method1

is defined in the interface, and I am annotating the implementation class, is there a way to make it work that way? Do other proxying mechanisms like transaction management / security work this way in other parts of spring correctly? And if I use the proxy interface, will the pointcut be specified when implementing the class that creates the pointcut? (I guess not since I don't use cglib)

+3


source to share


2 answers


try adding @Component to MyAspect class



@Component
@Aspect
public class MyAspect {
...

      

+3


source


just mark your aspect method with

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

      



Check out this one for a step by step guide

+2


source







All Articles