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

SpringBoot监听机制-以及使用

11-SpringBoot事件监听

Java中的事件监听机制定义了以下几个角色:

①事件:Event,继承 java.util.EventObject 类的对象

②事件源:Source ,任意对象Object

③监听器:Listener,实现 java.util.EventListener 接口 的对象

SpringBoot 在项目启动时,会对几个监听器进行回调,我们可以实现这些监听器接口,在项目启动时完成一些操作。

  • ApplicationContextInitializer、

  • SpringApplicationRunListener、

  • CommandLineRunner、

  • ApplicationRunner

    自定义监听器的启动时机:MyApplicationRunner和MyCommandLineRunner都是当项目启动后执行,使用@Component放入容器即可使用
    yApplicationRunner

/*** 当项目启动后执行run方法。* 缓存数据*/
@Component
public class MyApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("ApplicationRunner...run");System.out.println(Arrays.asList(args.getSourceArgs()));}
} 

MyCommandLineRunner

@Component
public class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("CommandLineRunner...run");System.out.println(Arrays.asList(args));}
}

MyApplicationContextInitializer的使用要在resource文件夹下添加META-INF/spring.factories

org.springframework.context.ApplicationContextInitializer=com.itheima.springbootlistener.listener.MyApplicationContextInitializer

@Component
public class MyApplicationContextInitializer implements ApplicationContextInitializer {@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {System.out.println("ApplicationContextInitializer....initialize");}
}

MySpringApplicationRunListener的使用要添加构造器

public class MySpringApplicationRunListener implements SpringApplicationRunListener {public MySpringApplicationRunListener(SpringApplication application, String[] args) {}@Overridepublic void starting() {System.out.println("starting...项目启动中");}@Overridepublic void environmentPrepared(ConfigurableEnvironment environment) {System.out.println("environmentPrepared...环境对象开始准备");}@Overridepublic void contextPrepared(ConfigurableApplicationContext context) {System.out.println("contextPrepared...上下文对象开始准备");}@Overridepublic void contextLoaded(ConfigurableApplicationContext context) {System.out.println("contextLoaded...上下文对象开始加载");}@Overridepublic void started(ConfigurableApplicationContext context) {System.out.println("started...上下文对象加载完成");}@Overridepublic void running(ConfigurableApplicationContext context) {System.out.println("running...项目启动完成,开始运行");}@Overridepublic void failed(ConfigurableApplicationContext context, Throwable exception) {System.out.println("failed...项目启动失败");}
}

12-SpringBoot流程分析-初始化

  1. 配置启动引导类(判断是否有启动主类)

  2. 判断是否是Web环境

  3. 获取初始化类、监听器类

在这里插入图片描述

13-SpringBoot流程分析-run

  1. 启动计时器

  2. 执行监听器

  3. 准备环境

  4. 打印banner:可以resource下粘贴自定义的banner

  5. 创建context

    refreshContext(context);
    

    执行refreshContext方法后才真正创建Bean

    在这里插入图片描述

14-SpringBoot监控-actuator基本使用

在这里插入图片描述

①导入依赖坐标

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>

②访问http://localhost:8080/acruator

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-component-instance":{"href":"http://localhost:8080/actuator/health/{component}/{instance}","templated":true},"health-component":{"href":"http://localhost:8080/actuator/health/{component}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false}}
}

http://localhost:8080/actuator/info

在application.properties中配置

info.name=lucy
info.age=99

http://localhost:8080/actuator/health

开启健康检查详细信息

management.endpoint.health.show-details=always
{"status":"UP","details":{"diskSpace":{"status":"UP","details":{"total":159579508736,"free":13558104064,"threshold":10485760}},"redis":{"status":"UP","details":{"version":"2.4.5"}}}
}

15-SpringBoot监控-actuator开启所有endpoint

开启所有endpoint

在application.properties中配置:

management.endpoints.web.exposure.include=*

开启所有endpoint的返回结果:

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"auditevents":{"href":"http://localhost:8080/actuator/auditevents","templated":false},"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},"caches-cache":{"href":"http://localhost:8080/actuator/caches/{cache}","templated":true},"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},"health-component-instance":{"href":"http://localhost:8080/actuator/health/{component}/{instance}","templated":true},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-component":{"href":"http://localhost:8080/actuator/health/{component}","templated":true},"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},"env":{"href":"http://localhost:8080/actuator/env","templated":false},"env-toMatch":{"href":"http://localhost:8080/actuator/env/{toMatch}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false},"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},"loggers-name":{"href":"http://localhost:8080/actuator/loggers/{name}","templated":true},"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},"metrics-requiredMetricName":{"href":"http://localhost:8080/actuator/metrics/{requiredMetricName}","templated":true},"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},"httptrace":{"href":"http://localhost:8080/actuator/httptrace","templated":false},"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}
}

16-SpringBoot监控-springboot admin图形化界面使用

SpringBoot Admin 有两个角色,客户端(Client)和服务端(Server)。

以下为创建服务端和客户端工程步骤:

admin-server:

①创建 admin-server 模块

②导入依赖坐标 admin-starter-server
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

      <dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId></dependency>

③在引导类上启用监控功能@EnableAdminServer

@EnableAdminServer
@SpringBootApplication
public class SpringbootAdminServerApplication {public static void main(String[] args) {SpringApplication.run(SpringbootAdminServerApplication.class, args);}}

admin-client:

①创建 admin-client 模块

②导入依赖坐标 admin-starter-client

  		<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId></dependency>

③配置相关信息:server地址等

# 执行admin.server地址
spring.boot.admin.client.url=http://localhost:9000management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*

④启动server和client服务,访问server

17-SpringBoot部署

SpringBoot 项目开发完毕后,支持两种方式部署到服务器:

①jar包(官方推荐)

②war包

更改pom文件中的打包方式为war

修改启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;@SpringBootApplication
public class SpringbootDeployApplication extends SpringBootServletInitializer {public static void main(String[] args) {SpringApplication.run(SpringbootDeployApplication.class, args);}@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {return builder.sources(SpringbootDeployApplication.class);}
}

指定打包的名称

 <build><finalName>springboot</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

相关文章:

SpringBoot监听机制-以及使用

11-SpringBoot事件监听 Java中的事件监听机制定义了以下几个角色&#xff1a; ①事件&#xff1a;Event&#xff0c;继承 java.util.EventObject 类的对象 ②事件源&#xff1a;Source &#xff0c;任意对象Object ③监听器&#xff1a;Listener&#xff0c;实现 java.util…...

若依学习——定时任务代码逻辑 详细梳理(springboot整合Quartz)

springboot整合Quartz关于若依定时任务的使用可以去看视频默认定时任务的使用关于springboot整合quartz的整合参考(150条消息) 定时任务框架Quartz-(一)Quartz入门与Demo搭建_quarzt_是Guava不是瓜娃的博客-CSDN博客(150条消息) SpringBoot整合Quartz_springboot quartz_桐花思…...

C++---最长上升子序列模型---拦截导弹(每日一道算法2023.3.4)

注意事项&#xff1a; 本题为"线性dp—最长上升子序列的长度"的扩展题&#xff0c;这里只讲贪心思路&#xff0c;dp去这个看。 题目&#xff1a; 某国为了防御敌国的导弹袭击&#xff0c;发展出一种导弹拦截系统。 但是这种导弹拦截系统有一个缺陷&#xff1a;虽然它…...

【机器学习面试】百面机器学习笔记和问题总结+扩展面试题

第1章 特征工程 1、为什么需要对数值类型的特征做归一化&#xff1f; &#xff08;1&#xff09;消除量纲&#xff0c;将所有特征统一到一个大致相同的区间范围&#xff0c;使不同指标之间具由可比性&#xff1b; &#xff08;2&#xff09;可以加快梯度下降收敛的速度&#…...

【2021.12.28】ctf逆向中的迷宫问题(含exe及wp)

【2021.12.28】ctf逆向中的迷宫问题&#xff08;含exe及wp&#xff09; 文章目录【2021.12.28】ctf逆向中的迷宫问题&#xff08;含exe及wp&#xff09;1、迷宫简介&#xff08;1&#xff09;简单例子&#xff08;2&#xff09;一般的迷宫代码2、二维迷宫&#xff08;1&#xf…...

WSL2使用Nvidia-Docker实现深度学习环境自由部署

1. Win11 显卡驱动的安装 注意&#xff1a;WSL2中是不需要且不能安装任何显卡驱动的&#xff0c;它的显卡驱动完全依赖于 Win11 中的显卡驱动&#xff0c;因此我们只需要安装你显卡对应的 Win11 版本显卡驱动版本&#xff08;必须是 Win11 版本的驱动&#xff09;&#xff0c;…...

SpringBoot入门 - 配置热部署devtools工具

在SpringBoot开发调试中&#xff0c;如果我每行代码的修改都需要重启启动再调试&#xff0c;可能比较费时间&#xff1b;SpringBoot团队针对此问题提供了spring-boot-devtools&#xff08;简称devtools&#xff09;插件&#xff0c;它试图提升开发调试的效率。准备知识点什么是…...

CANFDNET-200U-UDP配置与数据收发控制

一、启动ZCANPRP,打开设备管理页面&#xff0c;选择类型CANFDNET-200U-UDP,如图1 图1 二、打开设备&#xff0c;启动&#xff0c;在相应页面如图2&#xff0c;配置协议&#xff0c;CANFD 加速&#xff0c;本地端口&#xff0c;IP地址&#xff0c;工作端口。 图2 三、发送相应数…...

嵌入式中backtrace的使用

大家好&#xff0c;我是bug菌&#xff5e; backtrace主要用于调试程序时&#xff0c;能够打印出程序在运行过程中的函数调用栈&#xff0c;以帮助开发者快速定位程序出现异常或崩溃的原因。 通过backtrace的输出&#xff0c;开发者可以了解程序在哪个函数出现问题&#xff0c…...

CV学习笔记-Faster-RCNN

Faster R-CNN 文章目录Faster R-CNN1. 目标检测算法1.1 计算机视觉有五大应用1.2 目标检测任务1.3 目标检测算法概述2. 边框回归&#xff08;Bounding-Box regression&#xff09;2.1 IoU2.2 统计学中的指标2.3 边框回归3. Faster-RCNN网络3.1 Conv layers3.2 Region Proposal …...

大型三甲医院云HIS系统源码 强大的电子病历+完整文档

医院HIS系统源码云HIS系统&#xff1a;SaaS运维平台多医院入驻强大的电子病历完整文档 有源码&#xff0c;有演示 一、系统概述 采用主流成熟技术&#xff0c;软件结构简洁、代码规范易阅读&#xff0c;SaaS应用&#xff0c;全浏览器访问前后端分离&#xff0c;多服务协同&am…...

如何使用Spring Cloud搭建高可用的Elasticsearch集群?详解Elasticsearch的安装与配置及Spring Boot集成的实现

Spring Cloud 是一个基于 Spring Boot 的微服务框架&#xff0c;它提供了一系列组件和工具&#xff0c;方便开发人员快速搭建和管理分布式系统。Elasticsearch 是一个开源的全文搜索引擎&#xff0c;也是一个分布式、高可用的 NoSQL 数据库。本篇博客将详细讲解如何使用 Spring…...

phpinfo包含临时文件Getshell全过程及源码

目录 前言 原理 漏洞复现 靶场环境 源码 复现过程 前言 PHP LFI本地文件包含漏洞主要是包含本地服务器上存储的一些文件&#xff0c;例如session文件、日志文件、临时文件等。但是&#xff0c;只有我们能够控制包含的文件存储我们的恶意代码才能拿到服务器权限。假如在服…...

ubuntu22.04 Desktop 服务器安装

操作系统 使用的是Uubntu22.04 Desktop的版本&#xff0c;系统安装后&#xff0c;默认开启了53端口和631端口 关闭udp 5353、53791端口&#xff08;avahi-daemon服务&#xff09; sudo systemctl stop avahi-daemon.socket avahi-daemon.service sudo systemctl disable ava…...

Halcon——关于halcon中的一些语法

Halcon——关于halcon中的一些语法前言一、变量的创建与赋值二、if语句三、for语句四、while语句五、中断语句六、switch语句总结前言 在HDevelep环境下编程时&#xff0c;所用的一些语法与C#有些差异&#xff0c;在此做下记录。 一、变量的创建与赋值 Hdevelep中调用函数时&…...

Java 循环语句

Java 循环语句 循环语句就是在满足一定条件的情况下反复执行某一个操作的语句。Java中提供了3种常用的循环语句&#xff0c;分别是while循环语句、do…while循环语句和for循环语句。 1.while循环语句 while语句也称条件判断语句&#xff0c;它的循环方式为利用一个条件来控制…...

Python 基础语法

文章目录条件判断循环数据类型变量字符编码字符串格式化listtupledictset不可变对象”#“ 开头的是注释每一行是一个语句&#xff0c;当语句以冒号 “:” 结尾时&#xff0c;缩进的语句被视为代码块 好处&#xff1a;强迫代码格式化&#xff0c;强迫少用缩进 坏处&#xff1a;“…...

Kubernetes:通过 kubectl 插件 ketall 查看所有APi对象资源

写在前面 分享一个查看集群所有资源的小工具博文内容涉及&#xff1a; 下载安装常用命令 Demo 理解不足小伙伴帮忙指正 出其东门&#xff0c;有女如云。虽则如云&#xff0c;匪我思存。缟衣綦巾&#xff0c;聊乐我员。——《郑风出其东门》 分享一个查看集群所有资源的小工具&a…...

Zookeeper3.5.7版本——选举机制(非第一次启动)

目录一、ZooKeeper集群中哪些情况会进入Leader选举二、当一台机器进入Leader选举流程时&#xff0c;当前集群的两种状态2.1、集群中本来就已经存在一个Leader2.2、集群中确实不存在Leader三、Zookeeper中的一些概念了解3.1、SID3.2、ZXID3.3、Epoch一、ZooKeeper集群中哪些情况…...

Python | Leetcode刷题日寄Part05

欢迎交流学习~~ LeetCode & Python 系列&#xff1a; &#x1f3c6; Python | Leetcode刷题日寄Part01 &#x1f50e; Python | Leetcode刷题日寄Part02 &#x1f49d; Python | Leetcode刷题日寄Part03 ✈️ Python | Leetcode刷题日寄Part04 Python|Leetcode刷题日寄Par…...

LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器的上位机配置操作说明

LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器专为工业环境精心打造&#xff0c;完美适配AGV和无人叉车。同时&#xff0c;集成以太网与语音合成技术&#xff0c;为各类高级系统&#xff08;如MES、调度系统、库位管理、立库等&#xff09;提供高效便捷的语音交互体验。 L…...

pikachu靶场通关笔记22-1 SQL注入05-1-insert注入(报错法)

目录 一、SQL注入 二、insert注入 三、报错型注入 四、updatexml函数 五、源码审计 六、insert渗透实战 1、渗透准备 2、获取数据库名database 3、获取表名table 4、获取列名column 5、获取字段 本系列为通过《pikachu靶场通关笔记》的SQL注入关卡(共10关&#xff0…...

解读《网络安全法》最新修订,把握网络安全新趋势

《网络安全法》自2017年施行以来&#xff0c;在维护网络空间安全方面发挥了重要作用。但随着网络环境的日益复杂&#xff0c;网络攻击、数据泄露等事件频发&#xff0c;现行法律已难以完全适应新的风险挑战。 2025年3月28日&#xff0c;国家网信办会同相关部门起草了《网络安全…...

保姆级【快数学会Android端“动画“】+ 实现补间动画和逐帧动画!!!

目录 补间动画 1.创建资源文件夹 2.设置文件夹类型 3.创建.xml文件 4.样式设计 5.动画设置 6.动画的实现 内容拓展 7.在原基础上继续添加.xml文件 8.xml代码编写 (1)rotate_anim (2)scale_anim (3)translate_anim 9.MainActivity.java代码汇总 10.效果展示 逐帧…...

五子棋测试用例

一.项目背景 1.1 项目简介 传统棋类文化的推广 五子棋是一种古老的棋类游戏&#xff0c;有着深厚的文化底蕴。通过将五子棋制作成网页游戏&#xff0c;可以让更多的人了解和接触到这一传统棋类文化。无论是国内还是国外的玩家&#xff0c;都可以通过网页五子棋感受到东方棋类…...

C++_哈希表

本篇文章是对C学习的哈希表部分的学习分享 相信一定会对你有所帮助~ 那咱们废话不多说&#xff0c;直接开始吧&#xff01; 一、基础概念 1. 哈希核心思想&#xff1a; 哈希函数的作用&#xff1a;通过此函数建立一个Key与存储位置之间的映射关系。理想目标&#xff1a;实现…...

算法—栈系列

一&#xff1a;删除字符串中的所有相邻重复项 class Solution { public:string removeDuplicates(string s) {stack<char> st;for(int i 0; i < s.size(); i){char target s[i];if(!st.empty() && target st.top())st.pop();elsest.push(s[i]);}string ret…...

C++中vector类型的介绍和使用

文章目录 一、vector 类型的简介1.1 基本介绍1.2 常见用法示例1.3 常见成员函数简表 二、vector 数据的插入2.1 push_back() —— 在尾部插入一个元素2.2 emplace_back() —— 在尾部“就地”构造对象2.3 insert() —— 在任意位置插入一个或多个元素2.4 emplace() —— 在任意…...

P10909 [蓝桥杯 2024 国 B] 立定跳远

# P10909 [蓝桥杯 2024 国 B] 立定跳远 ## 题目描述 在运动会上&#xff0c;小明从数轴的原点开始向正方向立定跳远。项目设置了 $n$ 个检查点 $a_1, a_2, \cdots , a_n$ 且 $a_i \ge a_{i−1} > 0$。小明必须先后跳跃到每个检查点上且只能跳跃到检查点上。同时&#xff0…...

零基础在实践中学习网络安全-皮卡丘靶场(第十一期-目录遍历模块)

经过前面几期的内容我们学习了很多网络安全的知识&#xff0c;而这期内容就涉及到了前面的第六期-RCE模块&#xff0c;第七期-File inclusion模块&#xff0c;第八期-Unsafe Filedownload模块。 什么是"遍历"呢&#xff1a;对学过一些开发语言的朋友来说应该知道&…...