springboot项目启动加载数据库配置信息到redis ------R

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);
	} 
	
}

即可!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值