Spring AspectJ Style AOP
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开发者_如何学Python 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 target
pointcut 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:
@Pointcut(execution(* com.mycompany.MyClass.methodInAbstract()))
Changing it to
@Pointcut(execution(* com.mycompany.MyInterface.methodInAbstract()) && target(com.mycompany.MyClass))
worked well.
You probably don't have cglib in your CLASSPATH, this is because when you specify the proxy-target-class=true, a CGLIB based proxy is created, rather than the default java dynamic proxy based one. Can you try with CGLIB in the path or by removing the proxy-target-class attribute(assuming that your bean does have an interface which is needed for dynamic proxy to work).
EDIT 1: I tried your sample, and have put it in github at this location - git://github.com/bijukunjummen/mvc-samples.git, you can execute the test which exercises your scenario using - mvn test -Dtest=TestSpringAOP
and it seems to work well. Can you please review this test and see how different it is from your case.
精彩评论