springboot 使用CommandLineRunner 启动加载数据到redis缓存

博客主要围绕Spring Boot展开,介绍了使用CommandLineRunner在Spring Boot启动时将数据加载到Redis缓存的相关内容,涉及CommandLineRunner的用法,属于后端开发中Spring Boot与Redis结合的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot内置接口类CommandLineRunner 是在项目启动后执行的线程类
使用方法跟其他线程类一样只需要实现CommandLineRunner接口并 重写run方法


SpringBoot在项目启动后会遍历所有实现CommandLineRunner的实体类并执行run方法,如果需要按照一定的顺序去执行,那么就需要在实体类上使用一个@Order注解(或者实现Order接口)来表明顺序

下面代码是实现项目启动将省市区信息加载到redis缓存中代码示例:
/**
 * Created by HX on 2019/5/20.
 * 项目启动加载类
 */
@Component
@Slf4j
@Order(value=1)
public class MyStartupRunner implements CommandLineRunner {


    @Autowired
    ProvinceCityDistrictSerivce provinceCityDistrictSerivce;

    @Override
    public void run(String... args) throws Exception {
       provinceCityDistrictSerivce.initProvinceCityDistrictDtoList();
    }
}
@Service
@Slf4j
public class ProvinceCityDistrictServiceImpl implements ProvinceCityDistrictSerivce {

    @Autowired
    ProvinceCityDistrictMapper provinceCityDistrictMapper;
    @Autowired
    RedisService redisService;
  /**
     * 生成平铺省市区json数据
     * @return
     */
    @Override
    public String initProvinceCityDistrictDtoList(){
        log.info(">>>>>>>>>>开始加载平铺格式省市区信息<<<<<<<<<<<");
        ProvinceCityDistrict provinceCityDistrict = new ProvinceCityDistrict();
        List<ProvinceCityDistrictDto> provinceCityDistrictDtos = provinceCityDistrictMapper.selectDtoList(provinceCityDistrict);
        String s = JSON.toJSONString(provinceCityDistrictDtos);
        log.info(">>>>>>>>>>加载省市区信息完毕共计{}条<<<<<<<<<<<",provinceCityDistrictDtos.size());
        log.info(">>>>>>>>>>开始缓存省市区信息到redis<<<<<<<<<<<");
        redisService.set("PROVINCE_CITY_DISCTRICT_KEY",s);
        log.info(">>>>>>>>>>缓存省市区信息到redis完毕<<<<<<<<<<<");
        return s;
    }
}

 

可以使用Spring Boot提供的注解来实现将msg_info表的词条加载Redis中。具体步骤如下: 1. 在pom.xml文件中引入Redis依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在Spring Boot配置文件中配置Redis连接信息: ``` spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= ``` 3. 定义RedisTemplate,用于操作Redis: ```java @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } } ``` 4. 定义一个Service,用于从数据库中查询msg_info表的词条,并将其缓存Redis中: ```java @Service public class MsgInfoService { @Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired private MsgInfoMapper msgInfoMapper; public void loadMsgInfoToRedis() { List<MsgInfo> msgInfoList = msgInfoMapper.selectAll(); for (MsgInfo msgInfo : msgInfoList) { String key = "msg_info:" + msgInfo.getId(); redisTemplate.opsForValue().set(key, msgInfo); } } } ``` 5. 在Spring Boot启动类中调用loadMsgInfoToRedis方法: ```java @SpringBootApplication public class Application implements CommandLineRunner { @Autowired private MsgInfoService msgInfoService; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { msgInfoService.loadMsgInfoToRedis(); } } ``` 这样,在Spring Boot启动时会自动将msg_info表的词条加载Redis中,并以key+object的形式存储。可以使用redisTemplate操作Redis获取缓存数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值