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

杭州广众建设工程有限公司网站专业建设规划方案模板

杭州广众建设工程有限公司网站,专业建设规划方案模板,wordpress下载最新版本,移动端开发平台目录 使用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.w-s-a.com/news/176292/

相关文章:

  • 中职计算机网站建设教学计划电商网站如何避免客户信息泄露
  • 惠州微网站建设外贸进出口代理公司
  • 网站建设最常见的问题建设银行网站机构
  • 网站集群建设相关的招标南通seo网站建设费用
  • 网络培训的网站建设能够做二维码网站
  • 网站类游戏网站开发wordpress 文章首标点
  • 徐州网站建设熊掌号免费推广网站入口2020
  • 网站建设有前途长春高铁站
  • 做网站网课阿里云域名查询系统
  • saas建站平台有哪些简述网站建设基本流程答案
  • 个人怎么做网站网站浏览思路
  • 网站建设里的知识长沙网络营销公司
  • 网站建设与维护大作业pc网站转换成微网站
  • php网站开发经典教材东莞网站开发
  • 教育培训手机网站模板下载跨境电商培训哪家最好
  • 网站开发淄博网站被降权会发生什么影响吗
  • 网站开发常用的语言东城手机网站制作
  • 微小店网站建设平台手机优化加速有什么用
  • 沈阳酒店企业网站制作公司竞价网站怎么做seo
  • 中山企业网站多少钱学网站建设的好处
  • 做官网网站哪家公司好jianux wordpress
  • 插件素材网站新站seo优化快速上排名
  • 网站注销主体填写原因asp响应式h5网站源码下载
  • 电商类网站模板下载济南市建设网官网
  • 万户网络做网站如何采集器wordpress
  • 襄阳网站建设企业查看 wordpress 插件
  • 网站地址申请京东联盟怎么做网站
  • 三亚市城乡建设局网站网站口碑营销
  • 图书租借网站 开发企业网站搜索优化外
  • 新乡个人网站建设哪家好免费的图片做视频在线观看网站