InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Ramnivas Laddad on Making AOP Choices With AspectJ and Spring AOP

Posted by Srini Penchikala on Jan 05, 2009

Sections
Architecture & Design
Topics
Architecture ,
AOP
Tags
AspectJ ,
AJDT ,
Aspect Oriented Programming ,
AOP

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).

Srini Penchikala currently works as Security Architect and has 17 yrs of experience in software product management.

AspectJ Hybrid Load-Time Weaving by William Louth Posted
Re: AspectJ Hybrid Load-Time Weaving by Amin Mohammed-Coleman Posted
  1. Back to top

    AspectJ Hybrid Load-Time Weaving

    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
    www.jinspired.com/products/jxinsight/new-in-5.6...


    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

    by Amin Mohammed-Coleman

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

Educational Content

Jesper Boeg on Priming Kanban

In this interview, Jesper Boeg, author of the new InfoQ book – Priming Kanban, discusses the keys to using Kanban effectively, and how to get started if you are currently using other approaches.

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.