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

网站搭建接单功能性的网站归档系统

网站搭建接单,功能性的网站归档系统,个人网站模板怎么做,网站开发工程师职业定位前言: 工作中难免会遇到一些,程序启动之后需要提前执行的需求。 例如: 初始化缓存:在启动时加载必要的缓存数据。定时任务创建或启动:程序启动后创建或启动定时任务。程序启动完成通知:程序启动完成后通…

前言:

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

例如:

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

下面介绍几种常见方式:

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.yayakq.cn/news/790746/

相关文章:

  • 小网站开发框架制作一个景点的网站
  • 一键制作网站网络营销的主要工具有哪些
  • 云建站平台哪家好哪些网站可以做招生
  • 要建一个网站该怎么做应用软件免费下载
  • 怎么建设购物网站山东建设工程上传原件的网站
  • 口碑好的做pc端网站软件技术的发展趋势
  • 线上怎么做推广网站推广优化
  • 商城类网站方案海原县建设局网站
  • 广州做网站(信科网络)wordpress php apache
  • 怎样建外贸公司网站编辑公众号
  • 长沙网站制作一般多少钱学院网站怎么做的
  • 网站营销话术明企科技网站建设系统
  • 空间 网站都有 肿么做网站wordpress 问卷源码
  • 重庆招聘网站都有哪些徐州人才网最新招聘
  • 河南网络科技网站建设小程序在微信哪里找
  • 酒店网站 方案网站网页相关概念
  • 网站营销与推广wordpress如何修改首页模板文件
  • 弹出网站代码怎样优化自己的网站
  • 网站开发时自适应精湛的赣州网站建设
  • 蒙阴网站优化seo 优化 服务
  • 网站开发运营服务合同广告宣传模板在线制作免费
  • 西安米德建站网站后台管理系统页面
  • 成都网站只wordpress插件移植
  • 新注册公司网站建设公司网站建设服务类别
  • 中国有没有一家做茶叶的网站新建的网站只能用临时域名打开
  • ps手机网站页面设计珠海网站制作价格
  • 网站美工如何做外贸建站及推广
  • 阿里云申请域名优化电池充电是关闭还是打开好
  • 网站怎么设置二级域名wordpress直播接入
  • 企业网站建设条件东阳厂家高端网站设计