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

Spring事件监听器ApplicationListener

目录

介绍

spirng启动后启动某方法


介绍

ApplicationEvent以及Listener是Spring为我们提供的一个事件监听、订阅的实现,内部实现原理是观察者设计模式,设计初衷也是为了系统业务逻辑之间的解耦,提高可扩展性以及可维护性。事件发布者并不需要考虑谁去监听,监听具体的实现内容是什么,发布者的工作只是为了发布事件而已。

Spring提供的内置事件:

ContextRefreshedEvent:容器刷新事件
ContextStartedEvent:容器启动事件
ContextStoppedEvent:容器停止事件
ContextClosedEvent:容器关闭事件

spirng启动后启动某方法

package com.csc.coms.modules.job.util;import com.csc.coms.modules.task.service.TaskJobService;
import com.csc.coms.modules.taskconf.entity.TaskConf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;@Component
@Slf4j
public class InitJob implements ApplicationListener<ContextRefreshedEvent> {@Autowiredprivate TaskJobService taskJobService;@Overridepublic void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {try {init(contextRefreshedEvent);} catch (Exception e) {log.error("InitJob启动失败:{}", e);}}private void init(ContextRefreshedEvent contextRefreshedEvent) {System.out.println("InitJob定时任务------begin-------------");TaskConf taskConf = new TaskConf();taskJobService.startTaskJobHandle(taskConf);System.out.println("InitJob定时任务-------end------------------");}
}

http://www.lryc.cn/news/104993.html

相关文章:

  • 安全学习DAY10_HTTP数据包
  • 云原生落地实践的25个步骤
  • Stable diffusion 三大基础脚本 提示词矩阵,载入提示词,XYZ图表讲解
  • uniapp uni-combox 下拉提示无匹配项(完美解决--附加源码解决方案及思路)
  • 10. Mybatis 项目的创建
  • 历年 Nobel prize in Physics (诺贝尔物理学奖)简介
  • IDEA中Git面板操作介绍 变基、合并、提取、拉取、签出
  • Android Studio开发简易APP添加代办事项
  • python 统计所有的 仓库 提交者的提交次数
  • 018-从零搭建微服务-系统服务(五)
  • HarmonyOS 开发基础(三)登录页面单向数据绑定(父组件向子组件传参)
  • 发npm包
  • <el-empty>
  • IO流(4)- 序列化流与反序列化流
  • 人工智能如何应对 DevOps 监控和可观测性挑战
  • 数字化新时代,VR全景拍摄与制作
  • uniapp 权限说明
  • 3D Web轻量化渲染开发工具HOOPS Communicator是什么?
  • 心法利器[93] | 谈校招:技术面
  • excel英语翻译让你的数据更容易被理解
  • RK3588S之CPU--benchmark(二)
  • 入侵检查基础
  • JAVA开发工具-maven的安装与配置(最新最详细教程)
  • linux系统编程重点复习--进程的控制
  • 12-1_Qt 5.9 C++开发指南_自定义插件和库-自定义Widget组件(提升法(promotion)创建自定义定制化组件)
  • 【软件测试学习】—软件测试的基本认识(一)
  • Unity AI项目笔记
  • 如何在地图上寻找峨眉山零公里的龙洞湖
  • cloudstack平台host加入后,显示CPU speed为0GHz
  • 创新技术应用,提升企业图文档管理水平的新思路