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

长沙建站网站模板吉林电商网站建设价格

长沙建站网站模板,吉林电商网站建设价格,前端开发和后端开发前景,网站产品怎么改顺序目录 使用PostConstruct注解 实现InitializingBean接口 实现CommandLineRunner接口 实现ApplicationRunner接口 使用EventListener注解监听ApplicationReadyEvent事件 应用启动完成之前或者之后#xff0c;我们需要拿数据库中的一些数据加载到本地缓存中。这些数据一般都…目录 使用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() { // 从数据库中获取数据 ListYourData 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 { // 在所有属性被设置之后执行此方法 // 从数据库中获取数据并加载到缓存中 ListYourData 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 { // 从数据库中获取数据并加载到缓存中 ListYourData 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 { // 从数据库中获取数据并加载到缓存中 ListYourData 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() { // 从数据库中获取数据并加载到缓存中 ListYourData dataList yourRepository.findAll(); dataList.forEach(data - { cacheManager.getCache(yourCacheName).put(data.getId(), data); }); } } 需要注意的是加载数据的操作应该尽可能快地完成避免延迟应用的启动时间。如果加载数据量非常大你可能需要考虑异步加载或延迟加载的策略。
http://www.hkea.cn/news/14492985/

相关文章:

  • 免费公司企业建站代理上海企业网站建设公司哪家好
  • 襄阳大型网站建设昆明建设网站
  • 做app模板网站有哪些广东网站备案
  • 免费网站去哪找二手房出售
  • 比较好的网站搭建论坛php 网站开发收费
  • 二手房网站建设方案公司展示类网站模板
  • 网站申请免费wordpress 留言簿
  • 网站设计确认php做网站首页的代码
  • 西部数码助手网站后台管理网站建设模块需求分析
  • 免费网站教程wordpress 显示文章标签
  • 网站设计字体大小规范精品一卡2卡三卡4卡分类
  • 免费的域名注册网站宣传广告牌图片
  • 网站集成微信登陆网上申报系统
  • 网站和公众号的区别是什么意思枝江市住房和城乡建设局网站
  • 做网站分什么软件石家庄移动端网站建设
  • 韩国封号事件网站建设网站浮动窗口怎么做的
  • 网站不足网站开发程序哪个好
  • 大良网站建设广东的一起(17)做网站
  • 北京网站设计公司哪家公司好安阳市设计
  • 和布克赛尔网站建设动漫设计与制作专业的学校有哪些
  • 宿州网站公司青岛建设工程信息网
  • 扬州 网站建设828企业服务平台
  • wordpress 企业站旅游wordpress
  • 汉滨区住房和城乡建设局网站在阿里云安装wordpress
  • 网站首页图片大全网站地图 xml html
  • 备案网站建设方案模板吉林省吉林市有几个区
  • 郑州公司网站制作用ps做个人网站界面
  • 什么是 网站收录网络推广软件
  • 个人信息页面设计漂亮的网站如何进行推广
  • dw网页设计制作网站的成品新闻发稿公司