第二章 Spring Boot四大核心组件
文章目录
- 前言
- 一、Spring Boot Starter
- 1.1 Starter的应用示例
- 1.2 Spring Boot之前的Thymeleaf和Mybatis应用
- 1.2.1 Thymeleaf配置
- 1.2.2 Mybatis配置
- 1.2.3 小结
- 二、Spring Boot Autoconfigure
- 2.1 autoconfigure 简介
- 2.2 小结
- 三、Spring Boot CLI
- 四、Spring Boot actuator
- 4.1 添加依赖
- 4.2 配置需要开启监控的端点
- 4.3 启动服务并验证
- 4.3.1 启动结果
- 4.3.2 查看各个监控信息
- 总结
前言
上一章介绍了SpringBoot的作用以及一个简单web框架的搭建过程。那么本章来了解一下它到底有多少东西。 先透露一下,四大组件分别是:starter, autoconfigure, CLI 以及actuator。 下面我们就来详细介绍一些他们有什么用。
下一章就进入他们的详细源码结构中看看他们的本质了,期待ing。
一、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。
a、spring-boot-starter-thymeleaf
a、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文件,就是一个依赖传递包
。
二、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体现出来的能力的代码实现
三、Spring Boot CLI
Spring Boot CLI是一个命令行使用Spring Boot的客户端工具;主要功能如下:
- 运行groovy脚本 =>
官网2.1
- 打包groovy文件到jar =>
官网2.3
- 初始化Spring Boot项目 =>
官网2.4
- 其他
先上个官网文档:Spring Boot CLI 官网文档
因为这个我们用的比较少,所以就不多赘述了。个人感觉比较流脾的功能就是命令行直接执行groovy脚本了。
四、Spring Boot actuator
actuator是Spring Boot的监控插件,本身提供了很多接口可以获取当前项目的各项运行状态指标。
官网文档:Spring Boot actuator 官网文档
名词解释:
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查看官方文档了解或者留言一起研究一下,厚着脸皮我也没怎么用过这个。不过下一章介绍了starter和autoconfigure之后我们就可以去研究actuator的源码了。。。。
总结
本章主要介绍了Spring Boot的四大组件的作用,其中主要是starter和autoconfigure,另外的CLI和actuator用的并不多,所以没有仔细介绍。有想仔细了解的小伙伴可以留言。下一章分析Spring Boot Starter和Autoconfigure的源码实现并实现自己的starter
,敬请期待。。。
上一章:Spring Boot搭建一个WEB应用 & 什么是Spring Boot
下一章:揭开Spring Boot中的Starter神秘面纱并写一个我们自己的Starter
第二章 Spring Boot四大核心组件相关推荐
- SpringBoot学习笔记-2:第二章 Spring Boot 配置
第二章 Spring Boot 配置 1.YAML 配置 SpringBoot 全局配置文件 application.properties application.yml YAML 以数据为中心,比 ...
- spring boot @value_spring+vue全栈开发实战-第二章Spring Boot 基础配置-笔记0302-2020
Spring Boot 基础配置 1. Web 容器配置 2.Properties 配置 3.类型安全配置属性 1. Web 容器配置 a.常规配置 在 Spring Boot 项 目 中,可以内置 ...
- 19年8月 字母哥 第一章 spring boot 2.x基础及概念入门 这里全部看完了 热部署没出来 第二章在前面2页 用热点公司网不行
http://springboot.zimug.com/1233100 文档 http://www.zimug.com/page/5 字母哥个人博客 11111 第一章 spring bo ...
- 第五章 Spring Boot的数据库编程
若有错,请指出 第二章 搭建Springboot环境,配置视图解析器jsp页面 第三章 全注解下的Spring Ioc 第四章 约定编程-Spring AOP 第五章 Spring Boot的数据库编 ...
- Spring Boot 2.0 配置图文教程第 2 章 Spring Boot 配置## 书信息 demo.book.name=[Spring Boot 2.x Core Action] demo.b
本章内容 1.自定义属性快速入门 2.外化配置 3.自动配置 4.自定义创建 Starter 组件 摘录:读书是读完这些文字还要好好用心去想想,写书也一样,做任何事也一样 第 2 章 Spring B ...
- 19年8月 字母哥 第三章 spring boot 配置原理实战 用热点公司网不行
第三章 spring boot 配置原理实战 3.1.结合配置加载讲解bean自动装配原理 3.2.详解YAML语法及占位符语法 3.3.获取自定义配置的两种实现方法 3.4.配置文件注入值数据校验 ...
- 《深入理解 Spring Cloud 与微服务构建》第十六章 Spring Boot Security 详解
<深入理解 Spring Cloud 与微服务构建>第十六章 Spring Boot Security 详解 文章目录 <深入理解 Spring Cloud 与微服务构建>第十 ...
- 《Spring Boot极简教程》第8章 Spring Boot集成Groovy,Grails开发
第8章 Spring Boot集成Groovy,Grails开发 本章介绍Spring Boot集成Groovy,Grails开发.我们将开发一个极简版的pms(项目管理系统). Groovy和Gra ...
- 第5章 Spring Boot事务支持
开心一笑 [长得好看就出去走走,让其他人感受下外界的美好. 长得不好看就出去走走,让其他人感受下自己在外界的美好.] 新书购买 戳图购买 >>> 5.1 Spring事务介绍 5.1 ...
最新文章
- MAC中 jd-gui 下载java反编译的工具及安装
- android 加载动画效果_这效果炸了,网易云音乐“宇宙尘埃”特效
- 2013年全国首届CISA认证培训强化班成功举办
- python多线程怎么写日志_Python日志记录在多进程下的使用
- Centos 开放端口
- python psutil 获取命令历史_Python使用psutil获取进程信息的例子
- 用html css设计网站,HTMLCSS构建和设计网站
- Ubuntu Server 如何校对时钟(通过NTP)
- 用金蝶kis记账王批量审核会计凭证的方法
- 尚观python培训视频教程
- QQ群下载解除限速小技巧【提高千倍不止】【无需会员】
- python 中 函数的使用!!!
- 图的应用:骑士周游问题
- mysql数据库教学系统设计_MySQL-教学系统数据库设计
- python scipy.stats.norm.cdf_python scipy stats.norm用法及代码示例
- 中断系统的相关知识(五)(外部中断)
- 终于,数据中台成为3000万企业的增长引擎
- 基于单片机、RTOS玩MicroPython
- [4G5G专题-75]:流程 - 4G LTE无线接入网中运营商标识、基站标识、终端标识大全
- 一览29省2020年5G建设计划
热门文章
- 关于Unity中FPS第一人称射击类游戏制作(专题十)
- hosts文件不生效真正的解决方案
- python的drop duplicates_python – Pandas drop_duplicates方法不起作用
- C语言萨博,力求打造“国民奔驰C” 源于萨博基因的绅宝智道到底有何能耐?
- 投资自己的梦想—孙正义的投资哲学
- PEG 分子,Azido-PEG8-TFP ester,1818294-49-3结构特点
- 阴阳师手游服务器维护,《阴阳师》手游2月20日维护更新公告
- Event Source: WinMgmt ,Event ID: 47
- spi flash通用读写软IP
- 【AMAD】tenacity -- Python中一个专门用来retry的库