Listing 1: FNDTaskPuller class

 public class FNDTaskPuller {
    private SchedulingContext ctx;
    // Other code

    public FNDTaskPuller () {
        this.ctx = SchedulingCtxFactory.getInstance().getSchedulingContext();
        // Other code
    }

    public boolean someBizMethod() {
        // Method implementation
    }

    // Other code
}


Listing 2: FNDTaskPullerTest class

public void testSomeBizMethod () {
    FNDTaskPuller puller = new FNDTaskPuller();
    boolean result = puller.someBizMethod();

    // assertions goes after
    assertTrue(result);
    assertEquals(...);
}


Listing 3: CDBTaskPuller class gets ready for constructor injection

public class FNDTaskPuller {
    private SchedulingContext ctx;
    // Other code

    public void FNDTaskPuller (SchedulingContext ctx) {
        this.ctx = ctx;
    }

    // Other code
}


Listing 4: DAO method implementation without AOP

public void updateFNDData(FNDDataVO data) {
    Session session = null;
    Transaction tx = null;
    DataVO data = null;

    try {
        session = sessionFactory.openSession();
        tx = session.beginTransaction();

        session.update(FNDDataVO.class, data);

        session.flush();
        tx.commit();
    }
    catch (HibernateException e) {
        logger.debug("error updating FND data.", e);
        if (tx != null) tx.rollback();
    }
    finally {
        if (session != null) session.close();
    }
}


Listing 5: Example DAOFactory class

public class DAOFactory {
    // Code omitted here

    /**
     * Create the proxy instance based on the class name
     */
    public Object getDAOService(String daoClassName) {
        Object proxy = null;
        try {
            Class c = Class.forName(daoClassName);
            InvocationHandler h = new HibernateInvocationHandler(daoClassName);
            proxy = Proxy.newProxyInstance(c.getClassLoader(),
                                           c.getInterfaces(), h);
        }
        catch (ClassNotFoundException e) {
            logger.error("Error", e);
        }
        return proxy;
    }

    public class HibernateInvocationHandler implements InvocationHandler {
        private String daoClassName;
        public HibernateInvocationHandler(String daoClassName) {
            this.daoClassName = daoClassName;
        }

        public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {

            Class c = Class.forName(daoClassName);
            AbstractDAO target = (AbstractDAO)c.newInstance();

            Session session = null;
            Transaction tx = null;
            Object result = null;
            try {
                session = sessionFactory.openSession();
                tx = session.beginTransaction();
                target.setSession(session);

                result = method.invoke(target, args);

                session.flush();
                tx.commit();
            }
            catch (HibernateException e) {
                logger.error("Error. Transaction rolled back.", e);
                tx.rollback();
            }
            finally {
                if (session != null) session.close();
            }

            return result;
        }
    }
}


Listing 6: Example AbstractDAO class

public class AbstractDAO {
    private Session session;

    public void setSession(Session s) {
        this.session = s;
    }

    public Session getSession() {
        return this.session;
    }
}


Listing 7: Example DAO Interface

public interface MaterialDAO {
    public MaterialVO load(String id);
}


Listing 8: Example DAO implementation

public class MaterialDAOImpl extends AbstractDAO implements MaterialDAO {
    public MaterialVO load(String id) {
        return (MaterialVO)this.getSession().load(MaterialVO.class, id);
    }
}


Listing 9 Example client code

DAOFactory f = new DAOFactory();
MaterialDAO dao = (IMaterialDAO)f.getDAOService("MaterialDAOImpl");
MaterialVO material = dao.load("3");