import java.util.*; public aspect Persistence { private Set dirtyObjects = new HashSet(); // this pointcut identifies the domain objects pointcut domain(): within(domain.*); // every time an instance variable of a domain object o // is set, the object becomes dirty and needs to be added // to the dirtyObjects Set. pointcut dirty(Object o): domain() && this(o) && set(* *.*); // this pointcut specifies all the transactional methods, // after which the set of dirty objects must be written to // the database. pointcut transactional(): call(* Main.createCustomer(..)); // add the now dirty object o to dirtyObjects after(Object o): dirty(o) { dirtyObjects.add(o); } // a transactional method has completed; write all the dirty // objects to the database... after() returning: transactional() { for (Iterator i = dirtyObjects.iterator();i.hasNext();) { writeToDB(i.next()); } dirtyObjects.clear(); } private void writeToDB(Object o) { System.err.println("Writing " + o + " to database..."); } }