InfoQ

News

Ramnivas Laddad on Making AOP Choices With AspectJ and Spring AOP

Posted by Srini Penchikala on Jan 05, 2009 08:00 PM

Community
Architecture
Topics
AOP
Tags
Aspect Oriented Programming ,
AJDT ,
AOP ,
AspectJ

Spring AOP/AspectJ combination offers many choices, whether they are AOP system related (bytecode-based or proxy-based AOP), syntax related (traditional AspectJ, @AspectJ, or XML syntax) or weaving options (build time or load-time weaver), and a clear understanding of all those choices is very important to apply them pragmatically when using Aspects in enterprise applications.

Ramnivas Laddad said just one kind of AOP won't fit all applications and choosing the right combination will help developers be successful with AOP. He did a presentation at the recent SpringOne Americas conference on the various AOP design and implementation choices Spring AOP framework offers and the best practices in using it in the web applications.

Ramnivas discussed the advantages and limitations of AspectJ weaving and proxy-based Spring AOP approach. Choosing between Spring AOP and AspectJ depends on some design and environmental factors. Use Spring AOP in the following scenarios:

  • Method-only interception is sufficient.
  • Full power of AOP is overwhelming.
  • Don't want to use a special compiler such as AspectJ compiler (ajc).
  • Domain object cross-cutting is not needed.
  • Pre-written aspects meet your needs.

Use AspectJ AOP in all other scenarios. He listed some of the example applications of AspectJ (fine grained tracing and monitoring, domain objects, fine-grained security) and proxy AOP (transaction management, JMX monitoring, remoting, and security). In the presentation, Ramnivas showed the differences between traditional AspectJ and @AspectJ syntax options with code examples.

Aspect weaving choices include compile-time or load-time weaving (LTW). When using LTW option, you can use the Spring-driven LTW which allows aspect weaving without any container launch script modifications (no -javaagent) or using Spring's JPA agent using configuration option. The comparison between these two weaving choices is as follows:

Build-time (compile/binary):

  • Weaving cost at build time but full speed load-time.
  • Build system modifications are needed.
  • No deployment changes are required.
  • Best IDE support with Eclipse AspectJ Development Tools (AJDT).

Load-time:

  • Load-time speed, memory footprint affected.
  • Build system unchanged but deployment changes are needed to weave aspects.
  • No IDE tool support.

AOP design choices include the pointcut implementation utilizing the join point signatures and using wildcards to select a wide range of join points. Metadata (Annotations) can also be used to capture the join points, which offers the following advantages and disadvantages:

  • Advantages:
    Annotations are an easy way to capture certain crosscutting concerns in the applications. They help limit the collaboration between aspect and classes to just annotations.
  • Disadvantages:
    The collaboration from classes is needed. Also, the overuse of Annotations may obscure AOP's obliviousness property.

Some of the best practices in using metadata for AOP implementation are:

  • Annotations are best for expressing the inherent characteristics of a program element.
  • They should describe what is true at the join point - not what should happen at those points.
  • Avoid implementation-specific annotations
  • Annotations should describe what is true at the annotated element. For example, to capture a read only scenario, use @ReadOnly as the annotation name and not @TakeReadLock. And to mark a method that needs to be transactional, use @Transactional and not @JTATransactional.
  • Don't use annotations to create macros.
  • Use annotations that already exist like JPA (@Entity, @Table), JAX-WS (@WebService, @WebMethod), Spring (@Component, @Service, @Autowired, @ManagedResource).
AspectJ Hybrid Load-Time Weaving by William Louth Posted Jan 7, 2009 3:48 AM
Re: AspectJ Hybrid Load-Time Weaving by Amin Mohammed-Coleman Posted Jan 7, 2009 8:35 AM
  1. Back to top

    AspectJ Hybrid Load-Time Weaving

    Jan 7, 2009 3:48 AM by William Louth

    You can have the speed of build-time weaving and the flexibility of load-time weaving. Memory is also reduced. AspectJ Hybrid Load-Time Weaving http://www.jinspired.com/products/jxinsight/new-in-5.6.html

    JXInsight 5.6 extends it load-time weaving cache to now store the transformed bytecode image across application executions turning load-time weaving into binary weaving on consecutive executions. Our approach makes the switch between load-time weaving and binary weaving transparent - the first time an application is executed JXInsight will use load-time weaving the second time it uses binary weaving via the persisted transformed bytecode from the initial load-time weaving transformation. This reduces the (re)start-up time as well as the memory footprint - both which is extremely important in production environments.
    This is a much better solution and one that still allows you to turn off the instrumentation of particular libraries without requiring a complete application rebuild. We have various filtering mechanisms in front of our load-time weaving agent. In 5.7 we have additional load-time weaving optimizations including byte code compression. William

  2. Back to top

    Re: AspectJ Hybrid Load-Time Weaving

    Jan 7, 2009 8:35 AM by Amin Mohammed-Coleman

    Sounds very cool. Will be investigating this....

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.