Spirng的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()。
当我们想在spring初始化时做一些操作的时候,就可以实现这个接口。
例如:
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wangku.was.gohome.dao.ICacheTableDao;
@Service("initCatch")
public class InitCatch implements InitializingBean {
@Autowired
ICacheTableDao cacheTableDao;
@Override
public void afterPropertiesSet() throws Exception {
cacheTableDao.cacheDestCategory();
cacheTableDao.cacheMember();
cacheTableDao.cacheProductBasic();
cacheTableDao.cacheProductModify();
cacheTableDao.cacheSourceCategory();
cacheTableDao.cacheWangkuCategory();
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wangku.was.gohome.dao.ICacheTableDao;
@Service("initCatch")
public class InitCatch implements InitializingBean {
@Autowired
ICacheTableDao cacheTableDao;
@Override
public void afterPropertiesSet() throws Exception {
cacheTableDao.cacheDestCategory();
cacheTableDao.cacheMember();
cacheTableDao.cacheProductBasic();
cacheTableDao.cacheProductModify();
cacheTableDao.cacheSourceCategory();
cacheTableDao.cacheWangkuCategory();
}
}
上边所附代码在服务启动时做了加载缓存的操作。