Hi,
This is what we try to do:
- If a bean method has an annotation @MyAnnotation, wrap the method with Around Advice.
- Minimal performance impact on runtime calls to the beans.
First implementation was using AspectJ, with point cut:
PHP Code:
@Around("@annotation(com.blah.MyAnnotation)")
Very easy to set up, however performance was very slow. I benchmarked it. The reason as far as I can tell is that when you call the bean method, the proxy tries to assemble the list of aspects dynamically. Just put a breakpoint and see how much code runs when you call the bean method. Quite a lot. I could not find a way to make this assembly static, when the proxy is generated. It all happens at runtime each time the bean method is called.
More details:
http://forum.springframework.org/showthread.php?t=40417
Current implementation is this:
PHP Code:
<beans>
<!-- Enable Spring AOP support -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<property name="exposeProxy" value="false" />
<property name="frozen" value="true" />
<property name="opaque" value="false" />
<property name="optimize" value="true" />
</bean>
<!-- pointcut advisor -->
<bean class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut">
<bean class="...">
...
</bean>
</property>
<property name="advice">
<bean class="com.blah.MyMethodInterceptor" />
</property>
</bean>
</beans>
Performance is much better. The advice is resolved and configured at load time, and not when you call the bean method.
There is still an overhead of using the aopalliance MethodInterceptor interface and not the CGLIB comparable interface, but this is small comparing to resolving the list of aspects on every bean method call.
The impact on methods that are annotated is ~1:100, and methods that are not annotated in the same bean ~1:10. With AspectJ it was about an order of magnitude slower.
This is acceptable for the application. I think using the CGLIB interceptors directly can improve performance further. However, the major performance impact of dynamic aspect lookups on every method call is solved (verified using debugger). Currently this is sufficient.
If there is a way to use CBLIB directly I would still be interested to know, but its not so important now with the current benchmark results.
Thanks for the interest!
PS. PHP tag makes nice XML color coding