面试官:SpringBoot四大核心组件,你知道几个?

业余草

共 9293字,需浏览 19分钟

 ·

2021-08-18 17:40

你知道的越多,不知道的就越多,业余的像一棵小草!

你来,我们一起精进!你不来,我和你的竞争对手一起精进!

编辑:业余草

blog.csdn.net/u011909918

推荐:https://www.xttblog.com/?p=5261

前言

先透露一下,四大组件分别是:starter, autoconfigure, CLI 以及actuator。下面我们就来详细介绍一些他们有什么用。

一、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-xxxxxx-spring-boot-starter

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

「spring-boot-starter-thymeleaf」

spring-boot-starter-thymeleaf

在这里插入图片描述

「mybatis-spring-boot-starter」

mybatis-spring-boot-starter

在这里插入图片描述

「两种 starter 的区别就是 >>」

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

其中 xxx 就是我们想要依赖的组件或者 jar 包。上例就是我们 spring boot 用来引入 thymeleaf 引擎和 mybatis 框架所配置的依赖。引入之后通过简单的约定配置就可以正常使用。比如:

Thymeleaf 引擎约定配置:

##前端引擎配置
spring:
  thymeleaf:
    enabled: true
    servlet:
      content-type: text/html
    mode: HTML
    ## 页面前缀
    prefix: classpath:/templates/
    ## 后缀
    suffix: .html

Mybatis 约定配置:

mybatis:
  mapper-locations: classpath:mapper/*.xml  #注意:一定要对应mapper映射xml文件的所在路径
  type-aliases-package: com.hi.ld.vo.system  # 注意:对应实体类的路径
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

下面让我们来看看以前怎么配置 thymeleaf。

Spring Boot 之前的 Thymeleaf 和 Mybatis 应用

废话不多说,直接上代码:

Thymeleaf 配置

「添加对应依赖:」

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf-spring5</artifactId>
  <version>3.0.11.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.thymeleaf.extras</groupId>
  <artifactId>thymeleaf-extras-java8time</artifactId>
  <version>3.0.4.RELEASE</version>
</dependency>

「bean配置」

<bean id="templateResolver"  class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
  <property name="prefix" value="/WEB-INF/templates/" />
  <property name="suffix" value=".html" />
  <property name="templateMode" value="HTML5" />
</bean>

<bean id="templateEngine"
      class="org.thymeleaf.spring4.SpringTemplateEngine">

  <property name="templateResolver" ref="templateResolver" />
</bean>

<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
  <property name="templateEngine" ref="templateEngine" />
</bean>

Mybatis 配置

「添加对应依赖:」

```xmlorg.springframework.bootspring-boot-starter-jdbcorg.mybatismybatisorg.mybatismybatis-spring


**bean配置**

下面的第 3, 4 步骤就是 Mybatis 相关配置。第一步是引入资源配置。第二步是配置数据源

```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd"
>
   <!-- 配置整合mybatis过程 -->
   <!-- 1.配置数据库相关参数properties的属性:${url} -->
   <context:property-placeholder location="classpath:jdbc.properties" />

   <!-- 2.数据库连接池 -->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!-- 配置连接池属性 -->
    <property name="driverClass" value="${jdbc.driver}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />

    <!-- c3p0连接池的私有属性 -->
    <property name="maxPoolSize" value="30" />
    <property name="minPoolSize" value="10" />
    <!-- 关闭连接后不自动commit -->
    <property name="autoCommitOnClose" value="false" />
    <!-- 获取连接超时时间 -->
    <property name="checkoutTimeout" value="10000" />
    <!-- 当获取连接失败重试次数 -->
    <property name="acquireRetryAttempts" value="2" />
   </bean>

   <!-- 3.配置SqlSessionFactory对象 -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 注入数据库连接池 -->
    <property name="dataSource" ref="dataSource" />
    <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
    <property name="configLocation" value="classpath:mybatis-config.xml" />
    <!-- 扫描entity包 使用别名 -->
    <property name="typeAliasesPackage" value="com.soecode.lyf.entity" />
    <!-- 扫描sql配置文件:mapper需要的xml文件 -->
    <property name="mapperLocations" value="classpath:mapper/*.xml" />
   </bean>

   <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 注入sqlSessionFactory -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    <!-- 给出需要扫描Dao接口包 -->
    <property name="basePackage" value="com.soecode.lyf.dao" />
   </bean>
</beans>

小结

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

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

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

Spring Boot Autoconfigure

autoconfigure 简介

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

autoconfigure

在这里插入图片描述

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

「spring-boot-autoconfigure」

注意:这里有个点,就是官网提供的 configure 大多数在 spring-boot-autoconfigure 包里边,并没有单独创建新包。

spring-boot-autoconfigure

在这里插入图片描述

「mybatis-spring-boot-autoconfigure」

mybatis-spring-boot-autoconfigure

在这里插入图片描述

小结

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

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

Spring Boot CLI

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

  • 运行 groovy 脚本 => 官网 2.1
  • 打包 groovy 文件到 jar => 官网 2.3
  • 初始化 Spring Boot 项目 => 官网 2.4
  • 其他

先上个官网文档:

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-cli.html

因为这个我们用的比较少,所以就不多赘述了。个人感觉比较流脾的功能就是命令行直接执行 groovy 脚本了。

Spring Boot actuator

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

官网文档:

https://docs.spring.io/spring-boot/docs/2.4.0/reference/html/production-ready-features.html#production-ready

名词解释:

Endpoints: 需要监控的端点。参考官网第二节官网文档

「可用的端点」

可用的端点Endpoints
SpringBoot可用的端点Endpoints

下方的是 web 工程的端点。

使用方法如下:

添加依赖

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

配置需要开启监控的端点

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

启动服务并验证

启动结果

SpringBoot启动服务并验证

查看各个监控信息

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

查看监控信息

查看服务健康状态:

查看服务健康状态

其他 API 查看官方文档了解或者留言一起研究一下,厚着脸皮我也没怎么用过这个。不过下一章介绍了 starter 和 autoconfigure 之后我们就可以去研究 actuator 的源码了。。。。

总结

本章主要介绍了 Spring Boot 的四大组件的作用,其中主要是 starter 和autoconfigure,另外的 CLI 和 actuator 用的并不多,所以没有仔细介绍。

文章有帮助的话,在看,转发吧。谢谢支持哟 (*^__^*)

浏览 18
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报