1.sevice 下的common包下 :
步骤1:
package com.finance.cmp.dac.service.common;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.finance.cmp.dac.cache.content.Constant;
import com.finance.cmp.dac.common.enums.ValidStatus;
import com.finance.cmp.dac.dao.mapper.TChannelApiMapper;
import com.finance.cmp.dac.dao.mapper.TChannelMapper;
import com.finance.cmp.dac.dao.mapper.TDataConfigMapper;
import com.finance.cmp.dac.dao.mapper.TIntraAccessConfigMapper;
import com.finance.cmp.dac.dao.mapper.TSysConfigMapper;
import com.finance.cmp.dac.dao.model.TChannel;
import com.finance.cmp.dac.dao.model.TChannelApi;
import com.finance.cmp.dac.dao.model.TDataConfig;
import com.finance.cmp.dac.dao.model.TIntraAccessConfig;
import com.finance.cmp.dac.dao.model.TSysConfig;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class LoadRedisService {
@Autowired
private RedisCacheService redisCacheService;
@Autowired
private TSysConfigMapper tSysConfigMapper;
@Autowired
private TChannelApiMapper tChannelApiMapper;
@Autowired
private TChannelMapper tChannelMapper;
@Autowired
private TDataConfigMapper tDataConfigMapper;
@Autowired
private TIntraAccessConfigMapper tIntraAccessConfig;
public void loadRedisData() {
log.info("开始加载TSysConfig到redis=========");
List<TSysConfig> cfgList = tSysConfigMapper.selectAll().stream().filter((TSysConfig s) -> ValidStatus.VALID.getCode().equals(s.getStatus())).collect(Collectors.toList());
cfgList.forEach((TSysConfig ts) -> redisCacheService.set(Constant.REDIS_SYS_PREFIX + ts.getCfgKey(), ts));
log.info("加载TSysConfig到redis完成=========");
log.info("开始加载TChannel到redis=========");
List<TChannel> channelList = tChannelMapper.selectAll().stream().filter((TChannel s) -> ValidStatus.VALID.getCode().equals(s.getStatus())).collect(Collectors.toList());
channelList.forEach((TChannel ts) -> redisCacheService.set(Constant.REDIS_CHANNEL_PREFIX + ts.getChannelCode(), ts));
log.info("加载TSysCTChannelonfig到redis完成=========");
log.info("开始加载TIntraAccessConfig到redis=========");
List<TIntraAccessConfig> accessList = tIntraAccessConfig.selectAll().stream().filter((TIntraAccessConfig s) -> ValidStatus.VALID.getCode().equals(s.getStatus())).collect(Collectors.toList());
accessList.forEach((TIntraAccessConfig ts) -> redisCacheService.set(Constant.REDIS_ACCESS_PREFIX + ts.getSysCode(), ts));
log.info("加载TIntraAccessConfig到redis完成=========");
log.info("开始加载TChannelApi到redis=========");
List<TChannelApi> apiList = tChannelApiMapper.selectAll().stream().filter((TChannelApi s) -> ValidStatus.VALID.getCode().equals(s.getStatus())).collect(Collectors.toList());
redisCacheService.set(Constant.REDIS_CHANNELAPI_PREFIX , apiList);
log.info("加载TChannelApi到redis完成=========");
log.info("开始加载TDataConfig到redis=========");
List<TDataConfig> configList = tDataConfigMapper.selectAll().stream().filter((TDataConfig s) -> ValidStatus.VALID.getCode().equals(s.getStatus())).collect(Collectors.toList());
redisCacheService.set(Constant.REDIS_DATACONFIG_PREFIX , configList);
log.info("加载TDataConfig到redis完成=========");
}
}
步骤2 : sercive 的util 工具包下 :
package com.finance.cmp.dac.service.config;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.finance.cmp.dac.service.common.LoadRedisService;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class LoadDataUtil implements CommandLineRunner{
private static LoadRedisService loadRedisService;
@Override
public void run(String... arg0) throws Exception {
log.info(this.getClass().getName() + "启动加载数据" + arg0);
loadRedisService=getHomeBkService();
loadRedisService.loadRedisData();
}
private static LoadRedisService getHomeBkService() {
if (loadRedisService == null) {
loadRedisService = SpringBeanLocator.getBean(LoadRedisService.class);
}
return loadRedisService;
}
}
步骤3 : sercive 的util 工具包下 :
package com.finance.cmp.dac.service.config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;
@Component
public class SpringBeanLocator implements BeanFactoryAware {
private static BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
SpringBeanLocator.beanFactory = beanFactory;
}
public static <T> T getBean(String beanName) {
return (T) beanFactory.getBean(beanName);
}
public static <T> T getBean(Class beanType) {
return (T) beanFactory.getBean(beanType);
}
}
即可!