I have a Java app that uses Spring, and I have the aspect
@Aspect
public class MyAspect
{
@Pointcut("execution (* com.mycompany.MyClass.*(..))")
public void doStuff() {}
@Around("doStuff()")
public Object aroundDoStuff(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("before doStuff);
try
{
return pjp.proceed();
}
finally
{
System.out.println("after doStuff");
}
}
}
Then my spring bean file has
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="MyAspect"
class="com.mycompany.MyAspect" />
Now I would expect that all methods in MyClass get matched by the pointcut above, but that doesn’t seem to be the case (only one of the methods seems to have the advice applied). I’m not sure if this has to do with the fact that I’m proxying a class or not, but does anyone see what I might be doing wrong here?
EDIT: The code is called from a main class that does something like this:
ApplicationContext cxt; // lookup the cxt
MyClass mc = (MyClass) cxt.getBean("MyClassBean");
mc.doSomething(); // I expect the advice to be applied here.
thanks,
Jeff
It turned out the issue is because I was proxying a class and not an interface. I needed to change the pointcut to match all of the classes that implemented the interface and then used the
targetpointcut to filter down to MyClass.Edit: Adding details…
If MyClass extends AbstractMyClass and implements MyInterface, I wanted all methods in MyInterface to be advised, but this was not the case. I incorrectly declared my pointcut as:
Changing it to
worked well.