views:

89

answers:

1

We are moving our legacy implementation to Spring IBatis model. I am kind of stuck in modeling these objects in cleaner way using Spring model

Lets say I have two classes [ Both of them are singleton ]

DAOImpl implements DAOInterface

CacheDAOImpl implements DAOInterface

Code snippet showing object initialization in CacheDAOImpl

.....

private static CacheDAOImpl ourInstance = new CacheDAOImpl();

public static CacheDAOImpl  getInstance()
{
   return ourInstance;
}

private CacheDAOImpl()
{
 // intialiazes all caches
}

Code snippet from DAOImpl showing the CacheDAOImpl Object usage

private DAO getCacheDAO()
{
   return CacheDAOImpl.getInstance();
}

@Override
public SomeObject lookUpId()
{
  return getCacheDAO().lookUpId();
}

In the above implementation cache is initialized only when an method is invoked in DAOImpl whereas with Spring model of initialization, can we do this?. lazy-init may not work here as the object DAOImpl will always be accessed by non-lazy bean

A: 

First, spring's way of defining an object as singleton is to define in the singleton (which is default) bean scope.

Second, lazy-init should work. Just make the process of initialization the DAO bean differ from the process of using it. I.e. when it is constructed, don't initialize the cache - only when it's methods are called.

Bozho
@Bozho Thanks. It will be accessed by non-lazy bean always. I will edit the post for clarity.
Chandra