开发者

Lazy initialization of cache with Spring IBatis

开发者 https://www.devze.com 2022-12-17 03:55 出处:网络
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

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开发者_如何学Go 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消