ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Lazy Loading with Aspects
Subject:   a vastly simpler and more general AspectJ solution
Date:   2007-08-04 20:45:40
From:   jcaplan
I think I have a better way, but I invite comment on it:


http://jazzjuice.blogspot.com/2007/08/aspectj-for-lazy-loading-improved.html


The basic technique is to suppress the execution of the object's initialization code, wrapping it in a closure and storing it away until the first access to the object takes place. This is a pair of advice and two state variables, consisting of the closure and a flag indicating that the initialization has or has not been attempted, inside a single, small pertarget Director or abstract aspect (there are 2 alternative implementations). You can use this aspect without modification in most situations, marking whichever classes you want lazily instantiated in a separate application-specific aspect.