一篇文章带你了解Java SpringBoot四大核心组件

 更新时间:2021年9月13日 00:00  点击:1463

一、Spring Boot Starter

1.1 Starter的应用示例

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

在我们的Spring Boot项目种的POM文件中总会看到这两种依赖:

spring-boot-starter-xxx 和 xxx-spring-boot-starter。

这就是spring boot的四大组件之一的starter。

官方提供的starter是这样的:spring-boot-starter-xxx
非官方的starter是这样的:xxx-spring-boot-starter

总结:

a、Starter 帮我们封装好了所有需要的依赖,避免我们自己添加导致的一些Jar包冲突或者缺少包的情况;

b、Starter帮我们自动注入了需要的Bean实例到Spring 容器中,不需要我们手动配置(这个可以说是starter干的,实际上并不是,这里埋个坑,下面解答);

所以: starter包的内容就是pom文件,就是一个依赖传递包。

二、Spring Boot Autoconfigure

2.1 autoconfigure 简介

autoconfigure在我们的开发中并不会被感知,因为它是存在与我们的starter中的。所以我们的每个starter都是依赖autoconfigure的:

在这里插入图片描述

我们也可以把autoconfig的内容直接放在starter包里边。

autoconfigure内容是配置Bean实例到Spring容器的实际代码实现包,然后提供给starter依赖。所以说总结1的b项所说的配置Bean实例到Spring容器中实际是autoconfigure做的,因为是starter依赖它,所以也可以说是starter干的。

所以:autocinfigure是starter体现出来的能力的代码实现

三、Spring Boot CLI

Spring Boot CLI是一个命令行使用Spring Boot的客户端工具;主要功能如下:

运行groovy脚本

打包groovy文件到jar

初始化Spring Boot项目

可以命令行直接执行groovy脚本

四、Spring Boot actuator

actuator是Spring Boot的监控插件,本身提供了很多接口可以获取当前项目的各项运行状态指标。

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

management:
  endpoint:
    health: ## 开启健康监控端点
      enabled: true
    beans: ## 开启Bean实例监控端点
      enabled: true

浏览器访问(查看监控信息地址):http://localhost:9500/actuator

在这里插入图片描述

查看健康情况

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注猪先飞的更多内容!

[!--infotagslink--]

相关文章