190 likes | 373 Views
Comparison of Different AOP Approaches. Presented by: Xiaojing Wang. Outline. Problem Related work Comparison criteria AOP Approaches for comparison Results Conclusion. Problem. Not fully matured Evaluation is important. Related Work. Mik Kersten
E N D
Comparison of Different AOP Approaches Presented by: Xiaojing Wang
Outline • Problem • Related work • Comparison criteria • AOP Approaches for comparison • Results • Conclusion
Problem • Not fully matured • Evaluation is important
Related Work • Mik Kersten • AspectJ, AspectWerkz, JBossAOP, SpringAOP • how to handle AOP language mechanisms • how to integrate with existing development tools, environments, and libraries • Konstantin Ignatyev • CGLIB, Nanning, AspectWerkz,JBossAOP, and HiveMind • use advice to count the number of invocations of a TestInterface method • Alexandre Vasseur • AspectJ, AspectWerkz, JBossAOP, SpringAOP, CGLIB, and dynAOP • use AWbench to measure the relative performance of AOP/Interceptor frameworks
Comparison Criteria • Pointcut • Advice • Aspect declaration
Terms – 1 • Join point: well-defined points in execution of program • Pointcut: refers to collections of joint points and certain values at those join points • Advice: method-like constructs used to define additional behavior at joint points • Aspect: units of modular crosscutting implementation composed of pointcuts, advice, and ordinary Java member declarations.
Terms – 2 • Inter-type declaration (also called introduction): additional fields, methods, constructors for classes • Weave: the process of inserting aspect code into other code, can be done at build time, load time, and run time. • Interceptor: an Aspect with only one advice named "invoke". • Mixin: a kind of generic type, can improve the flexibility of Java class hierarchies, thereby improving the ability to modularize code and compose features
AOP Approaches for Comparison • AspectJ • JBossAOP • Nanning
AspectJ • Extension to Java • Define its own keyword to implement Aspect • Pointcuts: 8 types • Invocation, initialization, access, Exception handling, control flow, containment, conditional, and context • Pointcut operator: &&, || and ! • 3 types advice: before, after, and around
JBossAOP • plain Java class • Pointcuts • Pointcut operator: &&, || and ! • Advice • Using interceptor • Using XML or annotation • Only support around advice
Nanning • Consist of a set of mixins • Interface, target-object, interceptors • Pointcuts • Helper class P • Pointcut pointcut = P.methodName("set.*"); • Advice • Using interceptor • Using dynamic proxy
Aspect Declaration – AspectJ • Using Java-like code public class HelloWorld { public void say ( String msg ) { System.out.println ( msg ); } public static void main ( String[ ] args ) { new HelloWorld ( ).say ( "Hello, world! " ); } } public aspect HelloWorldAspect { pointcut sayPoint ( ): execution (void HelloWorld.say ( String )); after( ): sayPoint( ) { System.out.println( "Over!" ); } }
Aspect Declaration – JBossAOP // HelloWorldInterceptor.java import org.jboss.aop.advice.Interceptor; import org.jboss.aop.joinpoint.Invocation; public class HelloWorldInterceptor implements Interceptor { public String getName(){ return "HelloWorldInterceptor"; } public Object invoke (Invocation invocation) throws Throwable { System.out.print ("Hello, "); // invokes next interceptor or actual method return invocation.invokeNext(); } } // jboss-aop.xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <aop> <bind pointcut="execution(public void HelloWorld->say(java.lang.String))"> <interceptor class="HelloWorldInterceptor"/> </bind> </aop>
Aspect Declaration – Nanning AspectInstance instance = new AspectInstance(); Mixin mixin = new Mixin(Interface.class, new Target( )); Method method = Interface.class.getMethod("call", null); mixin.addInterceptor(method, new MethodInterceptor( ) { public Object invoke(Invocation invocation) { System.out.println("Hello World! " + invocation.getMethod() + " was called!"); return invocation.invokeNext( ); } }); instance.addMixin(mixin); Interface proxy = (Interface) instance.getProxy( ); proxy.call();
Conclusion • AspectJ provides most complet pointcut and advice • JBossAOP ignores some uncommonly used pointcut and only uses around advice • Nanning is not a mature tool, which uses dynamic proxy to express aspect instead of join points, pointcut, and advice. But it is simple to learn and use. • Recommend to use: AspectJ