SpringBoot基础(二)

简介: SpringBoot的监听机制其实是对Java提供的事件监听机制的封装Java的事件监听机制中定义了以下几个角色:事件(Event):继承Java.util.EventObject类的对象事件源(Source):任意对象Object监听器(Listener):实现java.util.EventListener接口的对象

SpringBoot基础+https://developer.aliyun.com/article/1390576?spm=a2c6h.13148508.setting.16.291b4f0es9dhYu

三、SpringBoot监听机制

1、事件监听

Java监听机制


SpringBoot的监听机制其实是对Java提供的事件监听机制的封装


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


事件(Event):继承Java.util.EventObject类的对象

事件源(Source):任意对象Object

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

SpringBoot监听机制


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


ApplicationContextInitializer、SpringApplicationRunListener、CommandLineRunner、ApplicationRunner



@

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


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


/**
 * 当项目启动后执行run方法 等同于ApplicationRunner
 */
@Component
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner");
    }
}


/**
 * 当项目启动后执行run方法 等同于CommandLineRunner
 * 一般可以用于redis缓存预热等(将数据库中某些数据提前存入redis)
 */
@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner");
    }
}


# resources下的META-INF目录下的spring.factories
org.springframework.boot.SpringApplicationRunListener=cn.ken.listener.MySpringApplicationRunListener
org.springframework.context.ApplicationContextInitializer=cn.ken.listener.MyApplicationContextInitializer

2、监控使用

使用步骤:

  1. 导入依赖坐标
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>


  1. 访问http://localhost:8080/actuator
  2. 配置



# info下的信息
info.name=zhangsan
info.age=13
# 开启健康检查的完整信息
# 默认关闭,因为上线时展示所有信息有风险
management.endpoint.health.show-details=always
# 将所有监控的endpoint暴露出来
management.endpoints.web.exposure.include=*


3、Spring Boot Admin

  • Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序
  • Spring Boot Admin有两个角色,客户端(Client)和服务端(Server)
  • 应用程序作为Spring Boot Admin Client向Spring Boot Admin Server注册
  • Spring Boot Admin Server的UI界面将Spring Boot Admin Client的Actuator Endpoint上的一些监控信息进行展示

使用步骤:

  1. admin-server:
  1. 创建admin-server模块
  1. 导入依赖坐标admin-starter-server
  2. 在引导类上启用监控功能@EnableAdminServer
  1. admin-client:
  1. 创建admin-client模块
  1. 导入依赖坐标admin-starter-client
  2. 配置相关信息:server地址等
  3. 启动server和client程序,访问server
server.port=8081
# 开启健康检查的完整信息
# 默认关闭,因为上线时展示所有信息有风险
management.endpoint.health.show-details=always
# 将所有监控的endpoint暴露出来
management.endpoints.web.exposure.include=*
# 注册到server上(url即为server的地址)
spring.boot.admin.client.url=http://localhost:8080



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


四、SpringBoot Web开发

1、静态资源导入

在SpringBoot,我们可以使用以下方式处理静态资源


  • webjars:localhost:8080/webjars/
  • public、static、resources:localhost:8080/


localhost:8080/webjars/jquery/3.4.1/jquery.js




localhost:8080/1.js

优先级:resources>static>public


2、Thymeleaf模板引擎

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>


随后将html页面放在templates文件夹下,将静态资源放在static文件夹下

templates下的资源不能直接访问,只能通过控制器

默认情况下:


public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";


五、SpringBoot项目部署

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

  1. jar包(官方推荐)
  2. war包

优先级:resources>static>public

2、Thymeleaf模板引擎

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>


随后将html页面放在templates文件夹下,将静态资源放在static文件夹下


templates下的资源不能直接访问,只能通过控制器


默认情况下:


public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";


五、SpringBoot项目部署

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

  1. jar包(官方推荐)
  2. war包


相关文章
|
监控 安全 Java
从0到1 手把手搭建spring cloud alibaba 微服务大型应用框架(十一)spring-boot-admin 监控篇(2)springcloud 集成spring boot admin
从0到1 手把手搭建spring cloud alibaba 微服务大型应用框架(十一)spring-boot-admin 监控篇(2)springcloud 集成spring boot admin
从0到1 手把手搭建spring cloud alibaba 微服务大型应用框架(十一)spring-boot-admin 监控篇(2)springcloud 集成spring boot admin
|
移动开发 JavaScript 小程序
从入门到实践:Uni-app跨平台开发与应用
从入门到实践:Uni-app跨平台开发与应用
910 1
springboot 获取访问接口的请求的IP地址
springboot 获取访问接口的请求的IP地址
3186 0
|
Java Shell Spring
java.lang.reflect.InaccessibleObjectException: Unable to make
java.lang.reflect.InaccessibleObjectException: Unable to make ... This exception occurs in a wide variety of scenarios when running an application on Java 9.
8025 0
|
10月前
|
Ubuntu Linux 程序员
无需编码5分钟免费部署云上调用满血版DeepSeek
大家好,我是V哥。本文介绍如何部署DeepSeek满血版,包括本地和云上两种方式。本地部署需准备Ubuntu/CentOS环境、硬件要求及Docker等工具,适合开发者;云上部署则推荐使用阿里云方案,最快5分钟、0元即可完成,提供100万免费token,无需编码,操作简便。选择适合自己的方式,体验高性能的DeepSeek模型吧!关注V哥,做个高效程序员。
1017 8
无需编码5分钟免费部署云上调用满血版DeepSeek
|
缓存 监控 Java
通用快照方案问题之Spring Boot Admin的定义如何解决
通用快照方案问题之Spring Boot Admin的定义如何解决
339 0
|
SQL 缓存 IDE
Spingboot项目解决.gitignore文件不起作用的问题
【10月更文挑战第15天】在使用 Git 时,有时会遇到 `.gitignore` 文件不起作用的情况。这通常是因为要忽略的文件或文件夹已经被添加到仓库中。解决方法是先将这些文件从缓存中移除,再重新提交。具体操作如下: 3. 运行 `git add .`。 4. 运行 `git commit -m "fixed files"`。 这样就可以成功忽略指定的文件或文件夹。
500 58
|
Linux 编译器 测试技术
【C++】CentOS环境搭建-快速升级G++版本
通过上述任一方法,您都可以在CentOS环境中高效地升级G++至所需的最新版本,进而利用C++的新特性,提升开发效率和代码质量。
696 64
|
Oracle 关系型数据库 Java
实时计算 Flink版操作报错合集之异常信息显示在Flink中找不到指定的ReplicationSlot如何解决
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
688 1
|
Java Linux
Centos安装openjdk11并配置JAVA_HOME
Centos安装openjdk11并配置JAVA_HOME
1382 0