spring加载InitializingBean时的循环依赖NPE问题记录

spring加载InitializingBean时的循环依赖NPE问题记录

afterPropertiesSet中全量加载缓存loadAllData
@Component
@EnableScheduling
public class Cache implements InitializingBean {
    ...
    @Resource
    private TimeConfigManager timeConfigManager;
    ...
    @Override
    public void afterPropertiesSet() throws Exception {
        CacheManager cacheManager = ehCacheCacheManager.getCacheManager();
        cache = cacheManager.getCache("cache");
        //加载所有数据
        loadAllData();
    }
此时cache类的timeConfigManager已经被注入,调用listAll方法,listAll中调用findAll,findAll调用dao层的timeConfigMapper类,此时mapper类(还没有被注入(解决循环依赖使用的二级缓存对象是没有完全初始化的对象)),导致报错空指针
@Service
public class TimeConfigManager extends BaseManager{

    @Resource
    private TimeConfigMapper timeConfigMapper;
    ...
    public List listAll(){
        ...
        findAll();
        ....
    }
    ...
    private List findAll(){
        ...
        timeConfigMapper.selectByExample(example);
        ...
    }

你可能感兴趣的:(java)