Hi friends,
Stuck on an issue, hoping you can assist:
I'm using Spring AOP to trigger events when annotated methods are called on some POJOs.
The POJO instances instantiated via applicationContext are CGLIB proxies, which is correct.
The problem I'm having is the POJO instances in calls to AfterReturningAdvice.afterReturning are NOT the proxy instances. Therefore, I'm unable to determine which instance triggered the event.
Some example code to explain further:
E.g.:
Code:<aop:config> <aop:advisor pointcut="execution(@com.example.Update * *(..))" advice-ref="updateAdvice" /> </aop:config> <bean id="updateAdvice" class="com.example.UpdateAdvice" /> <bean id="pojo" class="com.example.AopPojo" />Code:public class AopTest extends TestCase { public void testAop() throws Exception { AopPojo pojo = (AopPojo)context.getBean("pojo"); System.out.println(pojo.getClass()); // Output: class com.example.AopPojo$$EnhancerByCGLIB$$f08592d1 System.out.println(pojo.hashCode()); // Output: 47171681 pojo.setBlah("blah"); // method call triggers UpdateAdvice below } }
In the example above, the advice is triggered correctly. But as you see in the comments, the source object in the advice is not the proxy instance from the test case.Code:public class UpdateAdvice implements AfterReturningAdvice { public void afterReturning(Object returnValue, Method method, Object[] args, Object source) throws Throwable { System.out.println(source.getClass()); // Output: class com.example.AopPojo System.out.println(source.hashCode()); // Output: 3385016 } }
I can't figure out how to determine what the correct proxy instance is from this non-proxy instance (e.g. can't use == or .equals).
This is making it impossible to do message routing (PropertyChangeEvents in my case).
Any ideas on this? Note that the POJOs I'm annotating could be various types and do not implement a specific interface.
Thanks,
G


Reply With Quote
. 