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)
尝试将@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
链接地址: http://www.djcxy.com/p/77250.html上一篇: Spring AOP:@AfterThrowing执行切入点永远不会匹配
下一篇: Spring aspectj注释切入点