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:
- 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)
- 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)
- Â 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.
- 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: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();
 }
}
  <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:






































(4.75 out of 5)
2 Comments Received
Pingback & Trackback
Sorry the comment area are closed for non registered users