0

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

sth
  • 222,467
  • 53
  • 283
  • 367
Chandra
  • 333
  • 1
  • 15

1 Answers1

0

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
  • 588,226
  • 146
  • 1,060
  • 1,140