AOP – injecting behavior to your object


Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Introduction of AOP
Aspect oriented programming (AOP) is the great solution for the cross-cutting concerns like logging, security, auditing, exception handling, transaction and etc. Without AOP, those cross-cutting concern’s codes are spreading all over your system. Now, with AOP, you can dynamically inject behavior to your class (weaving) or object (proxy-based). The injection process is illustrated below:

aop.JPG

  • Pointcut – language construct that specifies join point
  • Join point – define specific points in program’s execution
  • Advice – define the pieces of an aspect implementation to be executed at pointcut
  • Aspect - combine these primitive

Spring proxy-based AOP vs AspectJ weaving (bytecode enhancement)

  1. Spring proxy-based AOP
    • Good: easy to use, no need post-compiler. Now it supports AspectJ pointcut expression. It is instance based.
    • Weak: carry overhead of reflection, no interception for internal call and limit of only method join points.
    • Usage: Service and Data Access Layer (not in domain layer)
  2.  AspectJ weaving
    • Good: achieve all the weakness in proxy-based AOP
    • Weak: need to do post compilation for your code for bytecode enhancement. It is type-based.
    • Usage: All layers including domain layer
    • Support for weaving (linking aspects with) binary class files either offline or at runtime as classes are loaded into the virtual machine.

Use Annotation vs XML for aspect wiring

  • Annotation approach -  Spring can actually understand @AspectJ aspects, so it is possible to share entire aspect definitions between Spring and AspectJ. Enabling this capability is easy, just include the <aop:aspectj-autoproxy> element in your configuration. If Aspectj-autoproxying is enabled in this way, then any bean defined in your application context with a declared type that is an @AspectJ aspect will be interpreted as an aspect by Spring AOP and beans in the context will be advised accordingly. However, you will have your POJO coupled with AspectJ annotation.
  •  <bean id=”helloService”
       class=”org.aspectprogrammer.hello.spring.HelloService”/>
    <aop:aspectj-autoproxy/>
    <bean id=”helloFromAspectJ”
       class=”org.aspectprogrammer.hello.aspectj.HelloFromAspectJ”/>

    Lets look at the POJO classes:

    public class HelloService {
      public void main() {
        System.out.println(“Hello World!”);
      }
    }

    @Aspect
    public class HelloFromAspectJ {
      @Pointcut(“execution(* main(..))”)
      public void mainMethod() {}

    @AfterReturning(“mainMethod()”)
      public void sayHello() {
      System.out.println(“Hello from AspectJ!”);
      }
    }

    //Test the AOP behavior injection
    public class SpringBoot {
      public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext(
      “org/aspectprogrammer/hello/spring/application-context.xml”);
      HelloService service = (HelloService) context.getBean(“helloService”);
      service.main();
      }
    }

  • XML Approach: Your POJO is agnositic about aspects. You will need to do the wiring in the XML. Here is the XML that does the same job as above: (It is centralized management with agnostic POJO but it can potentially complicate the already overloaded XML)
  •    <bean id=”helloService”
          class=”org.aspectprogrammer.hello.spring.HelloService”/>
       <aop:config>
         <aop:aspect ref=”helloFromSpringAOP”>
                 <aop:pointcut id=”mainMethod”
                              expression=”execution(* main(..))”/>
                 <aop:after-returning pointcut-ref=”mainMethod”
                              method=”sayHello”/>
         </aop:aspect>
       </aop:config>
       <bean id=”helloFromSpringAOP”
             class=”org.aspectprogrammer.hello.spring.HelloAspect”/>

Nice reference:

http://www.infoq.com/articles/Simplifying-Enterprise-Apps

Attached Files:

Leave a comment

1 Comments.

Leave a Reply

You must be logged in to post a comment.

Trackbacks and Pingbacks: