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

外贸网站建设公司报价主机屋网站

外贸网站建设公司报价,主机屋网站,手机网站设计软件,人力资源公司网站建设方案前言#xff1a; 工作中难免会遇到一些#xff0c;程序启动之后需要提前执行的需求。 例如#xff1a; 初始化缓存#xff1a;在启动时加载必要的缓存数据。定时任务创建或启动#xff1a;程序启动后创建或启动定时任务。程序启动完成通知#xff1a;程序启动完成后通…前言 工作中难免会遇到一些程序启动之后需要提前执行的需求。 例如 初始化缓存在启动时加载必要的缓存数据。定时任务创建或启动程序启动后创建或启动定时任务。程序启动完成通知程序启动完成后通过邮件、短信等方式通知运维人员。外部系统同步启动后与外部系统同步数据。 下面介绍几种常见方式 1.使用PostConstruct注解 使用PostConstruct注解可以在Spring容器初始化bean之后立即执行一个方法。这通常用于执行一些初始化任务如加载配置、初始化资源等。 import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;Component public class Test {PostConstructpublic void init(){System.out.println(Test类先初始化啦);} }2. 实现CommandLineRunner接口 CommandLineRunner接口允许你定义在Spring Boot应用程序启动后立即运行的方法。这个方法会接收一个字符串数组作为参数通常用于处理命令行参数。 package com;import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component;Component public class TestCommandLineRunner implements CommandLineRunner {Overridepublic void run(String... args) throws Exception {System.out.println(实现CommandLineRunner接口);} }3. 实现ApplicationRunner接口 ApplicationRunner接口与CommandLineRunner类似但它接收的参数是ApplicationArguments类型提供了更丰富的命令行参数处理功能。这对于需要更复杂的命令行参数解析的情况很有用。 package com;import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component;Component public class TestApplicationRunner implements ApplicationRunner {Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println(TestApplicationRunner实现ApplicationRunner接口);} }4.使用ApplicationListener或者使用事件注EventListener ApplicationStartedEvent事件 ApplicationStartedEvent事件在Spring Boot应用程序启动并开始运行但在任何HTTP服务器启动之前触发。 package com;import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component;Component public class TestApplicationListenerStartedEvent implements ApplicationListenerApplicationStartedEvent {Overridepublic void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {System.out.println(实现ApplicationListenerApplicationStartedEvent接口);}EventListener(classes {ApplicationStartedEvent.class})public void ApplicationStartedEvent(){System.out.println(采用事件监听注解EventListener监听ApplicationStartedEvent事件);} }ApplicationReadyEvent事件 ApplicationReadyEvent事件是在所有Spring Boot应用程序启动并且所有HTTP服务器都已经启动并监听端口后发布的事件。 package com;import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component;Component public class TestApplicationListenerReadyEvent implements ApplicationListenerApplicationReadyEvent {Overridepublic void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {System.out.println(实现ApplicationListenerApplicationStartedEvent接口);}EventListener(classes {ApplicationReadyEvent.class})public void ApplicationReadyEvent(){System.out.println(采用事件监听注解EventListener监听ApplicationReadyEvent事件);} }执行顺序 Test类先初始化啦------------ApplicationStartedEvent事件-------------------- 采用事件监听注解EventListener监听ApplicationStartedEvent事件 实现ApplicationListenerApplicationStartedEvent接口 ------------ApplicationStartedEvent事件--------------------TestApplicationRunner实现ApplicationRunner接口TestCommandLineRunner实现CommandLineRunner接口------------ApplicationReadyEvent事件-------------------- 采用事件监听注解EventListener监听ApplicationReadyEvent事件 实现ApplicationListenerApplicationStartedEvent接口 ------------ApplicationReadyEvent事件--------------------小结 选择哪种方式取决于你的具体需求例如是否需要处理命令行参数、是否需要在所有HTTP服务器都启动后执行代码、是否需要异步执行等。在大多数情况下PostConstruct、CommandLineRunner、ApplicationRunner和监听ApplicationReadyEvent事件是最常用的方法。一般生产中使用ApplicationRunner、CommandLineRunner因为其更加灵活。
http://www.hkea.cn/news/14513106/

相关文章:

  • 贵阳网站建设技术支持火山软件开发平台教程
  • 外贸网站 语言百度投放广告流程
  • 搜狐做app的网站网站收录查询情况
  • 手机网站分享js代码在住房和城乡建设部网站查询
  • 佛山微网站开发哪家好最好的免费cms系统
  • 滕州网站建网站的建设需要多少
  • 大连做网站公司WordPress 嵌入flash
  • 如何建立一个免费网站代理注册公司赚钱吗
  • 个人网站可以备案吗wordpress预约订单插件
  • 网站建设要不要工商注册西安公司电话
  • 一流的镇江网站优化wordpress调整logo大小
  • 广东官网网站建设公司西宁市城乡规划建设局网站
  • 网站正在维护中 模板网站后期增加产品
  • 保健品网站dede模板网络建设和网站建设
  • 作品展示html5网站模板jsp企业网站
  • 手机网站 免费个人网站备案需要什么资料
  • 中国网站为什么做的那么丑网站编辑兼职
  • 基于wordpress学校网站企业软件开发公司
  • codeorg免费编程网站有没有像一起做网店做男装的网站
  • 注册新公司网上核名网站优秀网站网址
  • app定制网站建设应有尽有沈阳酒店团购网站制作
  • 从事网站开发网站浏览历史能恢复吗怎么设置
  • 具有营销型网站的公司网迎客 网站建设
  • 如何做淘客网站个人网站seo入门
  • .net和php那个做网站好外链价格
  • 网站流量变现win7环境wordpress
  • 网站建设商家公司网页设计与制作教程第五版课后答案
  • 苏中建设 官方网站南昌建设局
  • 做乡村旅游的网站网站开发最快的语言
  • cc网站域名注册地图网站制作