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

网站建设基本流程视频品牌管理

网站建设基本流程视频,品牌管理,手机app游戏制作软件,网站头像有啥做会清晰前言: 工作中难免会遇到一些,程序启动之后需要提前执行的需求。 例如: 初始化缓存:在启动时加载必要的缓存数据。定时任务创建或启动:程序启动后创建或启动定时任务。程序启动完成通知:程序启动完成后通…

前言:

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

例如:

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

下面介绍几种常见方式:

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.tj-hxxt.cn/news/80332.html

相关文章:

  • wordpress获得分类目录二十条优化疫情措施
  • 大连百度关键词排名南沙seo培训
  • 杰诚网站建设网络推广平台软件app
  • 企业查查app下载免费seo推广计划
  • 如何改进网站免费学生网页制作成品代码
  • 网站建设市区爱上链外链购买交易
  • 做代购网站的纳税sem 优化软件
  • 什么是网站前置审批关键词排名seo优化
  • 深圳做门户网站的网络公司精准引流客源的方法可靠吗
  • 中国四大saas公司seo高手培训
  • 1空间做2个网站公关公司提供的服务有哪些
  • 网站怎么做反爬虫教育培训机构加盟
  • 网站建设系统 招标搜索引擎优化排名seo
  • 莒南网站建设注册城乡规划师
  • 大连中山区网站建设香港服务器
  • 棋牌网站网页设计与制作用什么软件
  • 知识付费网站源码影视后期培训机构全国排名
  • dedecms win8风格网站模板今日最火的新闻
  • 网站建设怎么样工作软文是什么
  • 网上花店网页制作素材上海专业的seo公司
  • 深圳做网站专业公司网络营销的发展概述
  • 在线代理服务器网站营销网页设计公司
  • 淘宝联盟怎么自己做网站推广手机百度账号申请注册
  • 腾讯云wordpress插件太原关键词优化软件
  • 服务器租用相关网站口碑营销的案例及分析
  • 郑州大型网站建设价格橙子建站官网
  • 怎么建立网站 个人热点济宁网站建设
  • 制作大型网站开发seo培训机构排名
  • 乌鲁木齐新市区建设局网站百度推广开户渠道
  • 登录信产部网站平台推广怎么做