Spring Boot 中的 4 大组件分别是:Spring Boot Starter、Spring Boot Autoconfigure、Spring Boot CLI 以及 Spring Boot actuator,接下来,我们分别来看他们的使用和作用。

1.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。

a、spring-boot-starter-thymeleaf

图片

b、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: trueservlet:content-type: text/htmlmode: 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。

1.2 Spring Boot之前的Thymeleaf和Mybatis应用

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

1.2.1 Thymeleaf配置

a. 添加对应依赖:

<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>

b. 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>

1.2.2 Mybatis配置

a. 添加对应依赖:

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

b. bean配置

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

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://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>

1.2.3 小结

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

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

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

2.Spring Boot Autoconfigure

2.1 autoconfigure 简介

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

图片

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

a. spring-boot-autoconfigure:

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

图片

b、mybatis-spring-boot-autoconfigure

图片

2.2 小结

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

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

3.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脚本了。

4.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: 需要监控的端点。参考官网第二节官网文档

可用的端点:

图片

图片

下方的是web工程的端点。

使用方法如下:

4.1 添加依赖

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

4.2 配置需要开启监控的端点

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

4.3 启动服务并验证

4.3.1 启动结果

图片

4.3.2 查看各个监控信息

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

图片

查看服务健康状态:

图片

其他API查看官方文档了解了。

总结

本章主要介绍了Spring Boot的四大组件的作用,其中主要是starter和autoconfigure,另外的CLI和actuator用的并不多,需要深入了解的小伙伴可以查看官方的API文档。

上期送书活动中奖名单

恭喜以下三位:

上期中奖规则为:第 6 名、第 16 名、第 26 名留言的用户,因为总留言数只有 20 条,所以把第 26 名中奖的用户直接改为最新留言的用户,恭喜以上三位。请以上中奖的用户加磊哥的微信号:GG_Stone 发送中奖信息和邮寄地址。其他小伙伴下次也积极参与哦,说不定下个中奖的人就是你呢?


往期推荐

SpringBoot时间格式化的5种方法!

SpringBoot 如何统一后端返回格式?老鸟们都是这样玩的!

SpringBoot 优雅的参数效验!

SpringBoot 中 4 大核心组件,你了解多少?相关推荐

  1. 成都大数据培训之SpringBoot中关于JDBC的方式运用

    在成都大数据培训中,Springboot中对于数据访问层,无论是SQL还是NOSQL,都默认采用整合Spring Data的方式进行统一处理,Springboot会帮我们添加大量自动配置,屏蔽了很多设 ...

  2. SpringBoot面试题大汇总附答案,SpringBoot面试题-持续更新中

    2021最新SpringBoot面试题[附答案解析]SpringBoot面试题及答案2021,SpringBoot2021最新面试题及答案,SpringBoot面试题新答案已经全部更新完了,有些答案是 ...

  3. 面试:SpringBoot中的条件注解底层是如何实现的?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来源 | https://urlify.cn/bm2qqi Spr ...

  4. springboot yml怎么建常量_【Java】SpringBoot 中从application.yml中获取自定义常量

    由于这里我想通过java连接linux,connection连接需要host.port.username.password及其他路径等等.不想每次修改的时候都去改源文件,所以想写在applicatio ...

  5. springboot中使用redis详解

    一.redis简介 redis是一款高性能key-value(键值对)内存型数据库,是非关系型数据库的一种,它采用单线程的架构方式,避免了多线程存在的锁处理造成的资源耗费,读取速度非常快,非常适合变化 ...

  6. Springboot中的缓存Cache和CacheManager原理介绍

    一.背景理解 什么是缓存,为什么要用缓存? 程序运行中,在内存保持一定时间不变的数据就是缓存.简单到写一个Map,里面放着一些key,value数据,就已经是个缓存了.所以缓存并不是什么高大上的技术, ...

  7. 从源码剖析SpringBoot中Tomcat的默认最大连接数

    为什么你的websocket只能建立256个连接?推出后,有许多小伙伴问:关键是怎么解决256这个问题.嗯,可能是我的标题起的有点问题,不过如果有认真阅读文章的话,应该会知道,其实256的限制是Chr ...

  8. SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理

    在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...

  9. 面试官 | SpringBoot 中如何实现异步请求和异步调用?

    作者 | 会炼钢的小白龙 来源 | cnblogs.com/baixianlong/p/10661591.html 一.SpringBoot中异步请求的使用 1.异步请求与同步请求 特点: 可以先释放 ...

最新文章

  1. 快速清理Android中无用的资源信息,图片,字符串等
  2. pytorch 常用的 loss function
  3. OllyDbg笔记-软件逆向调试技巧
  4. ES6语法---let和var的不同
  5. Dijkstra算法(求一点到任意一点的最短距离)
  6. linux源代码分析和阅读工具比较
  7. 【softmax分类】基于matlab梯度下降softmax回归minist数据分类【含Matlab源码 1645期】
  8. 土壤HWSD处理流程
  9. html中的js代码测试,w3school JS测验
  10. photoshop的抠图小结
  11. python学习----登陆
  12. 核心路由器市场分析:07回顾以及08展望
  13. PS教程:用Photoshop创建唯美月夜…
  14. 域名解析不生效,中科三方带你定位!
  15. 这种木头比钢和陶瓷更锋利,轻松切开半熟牛排,钉穿三层木板,还永不生锈 | Cell子刊...
  16. Python pandas 染色体 SNP 位点提取 并排序
  17. 马斯克:特斯拉正研发“世界上最好”的AI硬件
  18. 移动安全学习笔记——组件安全之ContentProvider组件漏洞挖掘
  19. mac 删除系统默认的ABC输入法
  20. 易语言连接mysql学习_[易语言]连接MYSQL数据库学习

热门文章

  1. 前端大框架知识归纳与总结
  2. FloatingActionMenu 向上弹出菜单
  3. CentOS7入门_安装并配置mysql5.7.18
  4. debian8.8安装谷歌浏览器
  5. Varnish缓存代理简介与配置
  6. Crawler - 如何爬取列表后进行文章的爬取
  7. cocos2dx3.2文件结构和代码结构
  8. 分析容灾备份建设需求
  9. 产品文档如何说清楚产品业务?关注这几点就够了
  10. Spark交互式分析平台Apache Zeppelin的安装