当前位置: 首页 > 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.dnsts.com.cn/news/197487.html

相关文章:

  • 寺庙网站开发建设方案wordpress留言板设置
  • 多媒体网站设计开发是指什么建网站做商城个体户资质可以
  • 免费网站建设能做吗网站设计流程的步骤
  • 贵阳市城乡建设部网站公司做一个静态网站多少钱
  • 个人网站开发是学什么语言做视频网站盈利模式
  • 做销售在哪个网站找客户端廊坊seo建站
  • 阿里巴巴国际网站建设wordpress+书店
  • 内蒙古住房与城乡建设厅网站网站公司 北京
  • 昆明网站运营临沂网站建设首选浩瀚网络
  • 宜昌网站建设宜昌南昌建站推广公司
  • 花卉网站建设项目策划书网站建设万禾
  • 我做的网站打开慢怎么处理产品展示型网站建设
  • 哔哩哔哩免费网站观看wordpress地址表单
  • 计算机应用技术php网站开发天津力天装饰有限公司
  • 专业低价建设微网站微商城怎么样河南洛阳网站建设
  • 重庆科技网站建设微信公众号商城搭建
  • 做网站 360seo关键词布局
  • 源码制作网站佛山哪个做网站的好
  • 诸城盟族网站建设苏州建设网站哪家好
  • 咸宁公司网站建设微网站管理平台
  • 大气的房产网站电子商务网站建设重要性
  • 上海宽带网网站哈尔滨网站建设优化
  • 沙坪坝做网站制作网站一般是多大
  • 做印刷的网站wordpress+微信悬浮
  • 曲靖做网站的公司企业域名怎么查找
  • 网站建设补充协议中英文网站英文
  • 文化书院网站建设方案但网页打不开
  • 长沙哪里可以做网站在线发外链工具
  • 做视频网站设备需求企业做一个app多少钱
  • 阜阳网站开发公司做一个15页的网站怎么做