6 Replies Latest reply on Nov 15, 2009 6:09 PM by Arbi Sookazian

    Interceptors vs. point cuts

    Arbi Sookazian Master

      AOP vs. interceptors topic (gentlemen, let's get ready to rumble!)


      So I was reading AspectJ in Action by RLaddad.  Very good book on AOP.  On pg. 21-22, the author describes EJB interceptor technology and why it is supposedly inferior to AOP b/c it does not support point cuts.



      Interceptor functionality is defined in the Java Interceptors specification. CDI enhances this functionality with a more sophisticated, semantic, annotation-based approach to binding interceptors to beans.

      source: weld-reference.pdf


      So the problem here (and plz correct me if I'm wrong) is with the annotation-based approach, you must apply the meta-data to each and every managed bean directly and manually rather than using the point cut expression which is more akin to implementing support for a cross-cutting concern like security, tx's, etc.


      For example, the pointcut expression:


      call(void Foo.make*(..)) || call(String Bar.get*(..))



      how would you implement a similar solution using CDI?  I'm guessing via XML?

        • 1. Re: Interceptors vs. point cuts
          Nicklas Karlsson Master

          Stereotypes will get you started.


          One option is to have an extension that listens to the beans at bootstrap and applies interceptor binding if the regexp matches, I suppose...

          • 2. Re: Interceptors vs. point cuts
            Gavin King Master

            For example, the pointcut expression:

            call(void Foo.make*(..)) || call(String Bar.get*(..))





            Now this is just a perfect example of broken AOP. By giving my method a name that begins with the common English word make, I totally change its semantics! How the f*** is someone coming along later and trying to maintain this awful mess supposed to know that if they change the name of this method, they will completely break their code?


            This is what your supposedly Very good book recommends? Well, I suppose they let just about anybody write books these days :-/


            Embedding special semantics into naming conventions is always a bad idea. Don't do it.


            No, that's not quite right. What I mean to say is: don't do it ever. And if anybody tells you to do it, ignore everything else they tell you.



            how would you implement a similar solution using CDI? I'm guessing via XML?

            You annotate the methods that this special semantic applies to @MySpecialSemantic. And your code is thereby maintainable.


            And you don't have to guess. You can read the docs or the spec to discover this.

            • 3. Re: Interceptors vs. point cuts
              Arbi Sookazian Master

              Gavin King wrote on Nov 14, 2009 21:38:


              Now this is just a perfect example of broken AOP. By giving my method a name that begins with the common English word make, I totally change its semantics! How the f*** is someone coming along later and trying to maintain this awful mess supposed to know that if they change the name of this method, they will completely break their code?

              This is what your supposedly Very good book recommends? Well, I suppose they let just about anybody write books these days :-/



              Ok, ok.  I knew you'd have a strong comeback on that one.  I don't know why, but I am partially fascinated by AOP.  It's like I try to find a way to make it seem like it's the preferable way of dealing with orthogonal or cross-cutting concerns.  This was the 2nd ed. of the book btw, it's a Manning book and I think most of the Manning books are very well written.  I find it interesting that it didn't cover Guice or CDI as far as I could tell.  But a book on CDI/Weld would not cover AOP either, no?


              But you have a very good point about breaking the code.  It's much easier to break the code in a project using XML (or separate types dedicated to advices, pointcuts, etc) rather than annotations.  Annotations are right in front of your eyes for the type in question and are type-safe.  What I'm concerned about with the annotational approach is having to apply the annotations to each type one by one.  But I guess I'm likely missing something (like stereotypes or @MySpecialSemantic?)


              But I'm still wondering if the author had a point about the lack of pointcuts in EJB interceptor (or CDI?) being a significant disadvantage.  I don't have the book in front of me but there is something about the join point context and having access to arguments.


              I'll be honest, AOP is definitely very complicated and overly complex.  AspectJ has its own ajc compiler as its an extension of Java but is not required with annotations.  I'm wondering how popular it will become ultimately (there was even a graph in the beginning of the book showing how popular AOP had become, then declined, then up again).  But even JBoss AOP is there.


              How does JBoss AOP interact with CDI?? or totally separate project?

              • 4. Re: Interceptors vs. point cuts
                Arbi Sookazian Master

                Apparently Bill Burke and others are interested in JBoss AOP:


                <aop>
                   <aspect class="org.jboss.aspects.OnewayAspect"/>
                
                   <bind pointcut="execution(void *->@org.jboss.Oneway(..))">
                      <advice name="oneway"
                              aspect="org.jboss.aspects.OnewayAspect"/>
                   </bind>
                </aop>



                http://www.jboss.org/jbossaop/docs/2.0.0.GA/docs/aspect-framework/userguide/en/html/annotations.html#annotations1


                there's that fantastical pointcut in full effect in JBoss AOP.


                who actually uses JBoss AOP, you don't really hear about it much...

                • 5. Re: Interceptors vs. point cuts
                  Gavin King Master

                  pointcut="execution(void *->@org.jboss.Oneway(..))"



                  there's that fantastical pointcut in full effect in JBoss AOP.

                  And here we have an example of good AOP. This pointcut is doing exactly what a CDI interceptor binding does. It is binding an interceptor, OnewayAspect, to methods with an annotation, @Oneway.


                  Of course, in CDI, you can do this without some ugly piece XML containing an embedded expression in some foreign expression language, and without needing to know what words like advice, point cut and aspect mean.

                  • 6. Re: Interceptors vs. point cuts
                    Arbi Sookazian Master

                    It would be beneficial if you wrote a blog comparing/contrasting Spring AOP or JBoss AOP (or AOP in general) vs. CDI interceptors.  With a concrete example, of course.  thx.