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

网站建设基本流程视频友情链接收录

网站建设基本流程视频,友情链接收录,做模具做什么网站,wordpress导航菜单 设置前言: 工作中难免会遇到一些,程序启动之后需要提前执行的需求。 例如: 初始化缓存:在启动时加载必要的缓存数据。定时任务创建或启动:程序启动后创建或启动定时任务。程序启动完成通知:程序启动完成后通…

前言:

工作中难免会遇到一些,程序启动之后需要提前执行的需求。

例如:

  • 初始化缓存:在启动时加载必要的缓存数据。
  • 定时任务创建或启动:程序启动后创建或启动定时任务。
  • 程序启动完成通知:程序启动完成后通过邮件、短信等方式通知运维人员。
  • 外部系统同步:启动后与外部系统同步数据。

下面介绍几种常见方式:

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 ApplicationListener<ApplicationStartedEvent> {@Overridepublic void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {System.out.println("实现ApplicationListener<ApplicationStartedEvent>接口>>>>>>>>>");}@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 ApplicationListener<ApplicationReadyEvent> {@Overridepublic void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {System.out.println("实现ApplicationListener<ApplicationStartedEvent>接口>>>>>>>>>");}@EventListener(classes = {ApplicationReadyEvent.class})public void ApplicationReadyEvent(){System.out.println("采用事件监听注解@EventListener,监听ApplicationReadyEvent事件");}
}

执行顺序:

Test类先初始化啦>>>>>>>>------------>ApplicationStartedEvent事件<--------------------
采用事件监听注解@EventListener,监听ApplicationStartedEvent事件
实现ApplicationListener<ApplicationStartedEvent>接口>>>>>>>>>
------------>ApplicationStartedEvent事件<--------------------TestApplicationRunner实现ApplicationRunner接口>>>>>TestCommandLineRunner实现CommandLineRunner接口>>>>>>------------>ApplicationReadyEvent事件<--------------------
采用事件监听注解@EventListener,监听ApplicationReadyEvent事件
实现ApplicationListener<ApplicationStartedEvent>接口>>>>>>>>>
------------>ApplicationReadyEvent事件<--------------------

小结:

选择哪种方式取决于你的具体需求,例如是否需要处理命令行参数、是否需要在所有HTTP服务器都启动后执行代码、是否需要异步执行等。在大多数情况下,@PostConstructCommandLineRunnerApplicationRunner和监听ApplicationReadyEvent事件是最常用的方法。一般生产中使用ApplicationRunner、CommandLineRunner,因为其更加灵活。

http://www.shuangfujiaoyu.com/news/22734.html

相关文章:

  • 肇庆住房和城乡建设部网站公众号怎么开通
  • 推广整合营销seo优化网站推广全域营销获客公司
  • 建设银行信用卡管理中心网站首页品牌策划方案ppt
  • 从化哪里做网站好网络游戏排行榜百度风云榜
  • 运城有做网站设计百度指数数据分析平台官网
  • 代理记账公司利润大吗热狗seo外包
  • 人防工程做资料的网站搜索引擎优化技术
  • 企业专业网站设计公公司网站推广费用
  • 业余做衣服的网站2024新闻热点事件
  • 冠县建设局网站seo网站
  • 怎么做自己网站里的资讯2023年小学生简短小新闻
  • 什么是网站评价郑州百度seo网站优化
  • 建设个电商平台网站需要多少钱广州网站seo推广
  • 番禺外贸网站建设宁波关键词优化排名工具
  • java ee只是做网站吗百度网盘电话人工服务
  • 独立网站电子商务公司广州做seo的公司
  • 公司网站门户建设包括哪些句容市网站seo优化排名
  • 网站首屏做多大seo实战培训机构
  • 网站建设中企动力强信息推广平台有哪些
  • 网站设计怎么做背景颜色谷歌外贸平台推广需要多少钱
  • 潍坊做网站中国搜索
  • 网站开发的技术手段有哪些网络营销成功的案例及其原因
  • 金融软件网站建设公司seo快速优化软件
  • 深圳住建官网廊坊优化技巧
  • 公司手机网站设计百度软件应用市场
  • 网站业务需求文档百度推广一年大概需要多少钱
  • 做一个web网站小吃培训
  • 网站重要组成部分uc浏览器网页版入口
  • 购物网站创业时是如何做宣传的房地产网站建设
  • 哪家做企业网站找片子有什么好的关键词