当前位置: 首页 > news >正文

springboot启动加载

目录

使用@PostConstruct注解

实现InitializingBean接口

实现CommandLineRunner接口

实现ApplicationRunner接口

使用@EventListener注解监听ApplicationReadyEvent事件


应用启动完成之前或者之后,我们需要拿数据库中的一些数据加载到本地缓存中。这些数据一般都是不经常变更而且有被经常读取的,并且数据量不是很大的。这样操作可以减少一些数据库的压力。下面是我们常用的实现方式,具体的要用那种,看对应各自应用的设计和需求。

使用@PostConstruct注解


在需要加载数据的组件中,可以使用@PostConstruct注解的方法,该方法会在Spring容器初始化该类之后自动调用

import javax.annotation.PostConstruct;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Component;  @Component  
public class CacheInitializer {  @Autowired  private YourRepository yourRepository; // 假设你有一个访问数据库的Repository  @Autowired  private CacheManager cacheManager; // 假设你使用Spring Cache进行缓存管理  @PostConstruct  public void init() {  // 从数据库中获取数据  List<YourData> dataList = yourRepository.findAll();  // 将数据加载到缓存中  dataList.forEach(data -> {  cacheManager.getCache("yourCacheName").put(data.getId(), data);  });  }  
}

实现InitializingBean接口

它只包含一个方法 afterPropertiesSet()。当一个 bean 的所有属性都被 Spring 容器设置之后(即通过依赖注入),afterPropertiesSet() 方法会被自动调用

import org.springframework.beans.factory.InitializingBean;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Component;  @Component  
public class CacheInitializingBean implements InitializingBean {  @Autowired  private YourRepository yourRepository; // 假设你有一个访问数据库的Repository  @Autowired  private CacheManager cacheManager; // 假设你使用Spring Cache进行缓存管理  @Override  public void afterPropertiesSet() throws Exception {  // 在所有属性被设置之后,执行此方法  // 从数据库中获取数据并加载到缓存中  List<YourData> dataList = yourRepository.findAll();  dataList.forEach(data -> {  cacheManager.getCache("yourCacheName").put(data.getId(), data);  });  }  
}

注:使用 InitializingBean 接口与使用 @PostConstruct 注解的效果类似,但它们在 Spring 生命周期中的调用时机略有不同。@PostConstruct 注解的方法是在依赖注入完成后立即调用,而 InitializingBean 的 afterPropertiesSet() 方法则是在所有属性被设置之后调用。在大多数情况下,这两个选项可以互换使用,具体选择哪个取决于你的个人喜好和项目需求。

实现CommandLineRunner接口


CommandLineRunner接口提供了一个run方法,该方法会在Spring Boot应用启动后立即执行。

import org.springframework.boot.CommandLineRunner;  
import org.springframework.stereotype.Component;  @Component  
public class CacheRunner implements CommandLineRunner {  @Autowired  private YourRepository yourRepository;  @Autowired  private CacheManager cacheManager;  @Override  public void run(String... args) throws Exception {  // 从数据库中获取数据并加载到缓存中  List<YourData> dataList = yourRepository.findAll();  dataList.forEach(data -> {  cacheManager.getCache("yourCacheName").put(data.getId(), data);  });  }  
}

实现ApplicationRunner接口


ApplicationRunner接口类似于CommandLineRunner,但提供了更灵活的参数处理方式

import org.springframework.boot.ApplicationArguments;  
import org.springframework.boot.ApplicationRunner;  
import org.springframework.stereotype.Component;  @Component  
public class CacheApplicationRunner implements ApplicationRunner {  @Autowired  private YourRepository yourRepository;  @Autowired  private CacheManager cacheManager;  @Override  public void run(ApplicationArguments args) throws Exception {  // 从数据库中获取数据并加载到缓存中  List<YourData> dataList = yourRepository.findAll();  dataList.forEach(data -> {  cacheManager.getCache("yourCacheName").put(data.getId(), data);  });  }  
}

使用@EventListener注解监听ApplicationReadyEvent事件


Spring Boot在启动完成后会发布一个ApplicationReadyEvent事件,你可以监听这个事件并在事件触发时执行加载数据的逻辑。

import org.springframework.boot.context.event.ApplicationReadyEvent;  
import org.springframework.context.event.EventListener;  
import org.springframework.stereotype.Component;  @Component  
public class CacheInitializer {  @Autowired  private YourRepository yourRepository;  @Autowired  private CacheManager cacheManager;  @EventListener(ApplicationReadyEvent.class)  public void onApplicationReadyEvent() {  // 从数据库中获取数据并加载到缓存中  List<YourData> dataList = yourRepository.findAll();  dataList.forEach(data -> {  cacheManager.getCache("yourCacheName").put(data.getId(), data);  });  }  
}

需要注意的是,加载数据的操作应该尽可能快地完成,避免延迟应用的启动时间。如果加载数据量非常大,你可能需要考虑异步加载或延迟加载的策略。

http://www.lryc.cn/news/311253.html

相关文章:

  • 基于Java的智能停车场管理系统(Vue.js+SpringBoot)
  • ESD Clamp cell是什么?
  • 费率电能表
  • 2张图2秒钟3D重建!这款AI工具火爆GitHub,网友:忘掉Sora
  • C++高级面试题:请解释 C++ 中的指针和引用之间的区别。
  • Git 配置处理客户端无法正常访问到 github 原网站时,npm 下载依赖包失败的问题
  • 前端爬虫+可视化Demo
  • keepAlive
  • 蓝桥杯练习题——dp
  • kotlin基础语法
  • 淘宝天猫商家爬虫工具 电商采集软件使用教程
  • 建库建表时,最容易忽略的10个细节
  • 【基础知识】什么是 PPO(Proximal Policy Optimization,近端策略优化)
  • 程序员如何选择职业赛道?
  • [LeetBook]【学习日记】寻找和为指定数字的连续数字
  • 阿里云中小企业扶持权益
  • 2核4g服务器能支持多少人访问?并发数性能测评
  • Anthropic官宣Claude3:建立大模型 推理、数学、编码和视觉等方面 新基准
  • STM32 TIM编码器接口
  • Jupyter Notebook的安装和使用(windows环境)
  • Platformview在iOS与Android上的实现方式对比
  • 使用lnmp环境部署laravel框架需要注意的点
  • AI-RAN联盟在MWC24上正式启动
  • Reactor详解
  • 实践航拍小目标检测,基于YOLOv5全系列【n/s/m/l/x】参数模型开发构建无人机航拍场景下的小目标检测识别分析系统
  • 分布式数据库中全局自增序列的实现
  • 【论文阅读】TensoRF: Tensorial Radiance Fields 张量辐射场
  • 深入了解 Android 中的 FrameLayout 布局
  • 高级大数据技术 实验一 scala编程
  • 使用Fabric创建的canvas画布背景图片,自适应画布宽高