上一篇:深夜看了张一鸣的微博,让我越想越后怕

前言

先透露一下,四大组件分别是: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-xxx 和 xxx-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文件,就是一个依赖传递包。

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

  • 其他

先上个官网文档:

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

可用的端点:

下方的是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用的并不多,所以没有仔细介绍。

(感谢阅读,希望对你所有帮助)

来源:blog.csdn.net/u011909918/

article/details/109647196

感谢您的阅读,也欢迎您发表关于这篇文章的任何建议,关注我,技术不迷茫!小编到你上高速。

· END ·

最后,关注公众号互联网架构师,在后台回复:2T,可以获取我整理的 Java 系列面试题和答案,非常齐全。

正文结束

推荐阅读 ↓↓↓

1.不认命,从10年流水线工人,到谷歌上班的程序媛,一位湖南妹子的励志故事

2.如何才能成为优秀的架构师?

3.从零开始搭建创业公司后台技术栈

4.程序员一般可以从什么平台接私活?

5.37岁程序员被裁,120天没找到工作,无奈去小公司,结果懵了...

6.IntelliJ IDEA 2019.3 首个最新访问版本发布,新特性抢先看

7.这封“领导痛批95后下属”的邮件,句句扎心!

8.15张图看懂瞎忙和高效的区别!

一个人学习、工作很迷茫?

点击「阅读原文」加入我们的小圈子!

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

  1. SpringBoot四大核心组件,必知必会!

    欢迎关注方志朋的博客,回复"666"获面试宝典 前言 先透露一下,四大组件分别是:starter, autoconfigure, CLI 以及actuator.下面我们就来详细介绍 ...

  2. SpringBoot四大核心

    SpringBoot四大核心 SpringBoot四大核心 1.自动配置 1.1application.properties配置文件 1.2application.yml配置文件 1.3applica ...

  3. Mybatis中SqlSession下的四大核心组件分析

    SqlSession下的四大核心组件 Mybatis中SqlSession下的四大核心组件:ParameterHandler .ResultSetHandler .StatementHandler . ...

  4. 学习笔记之Android四大核心组件详解

    概述 Android四大核心组件指的是Activity,Service,ContentProvider,BroadCastReceiver,核心组件都是由Android系统进行管理和维护的,一般都要在 ...

  5. SpringBoot四大核心之自动装配——源码解析

    四大核心 1.自动装配:简单配置甚至零配置即可运行项目 2.Actuator:springboot程序监控器 3.starter:jar包的引入,解决jar版本冲突问题 4.CLI:命令行 初学体验 ...

  6. SpringBoot四大核心之actuator——程序监控器

    四大核心 1.Actuator:springboot程序监控器 2.自动装配:简单配置甚至零配置即可运行项目 3.starter:jar包的引入,解决jar版本冲突问题 4.CLI:命令行 actua ...

  7. SpringBoot四大核心之starter——自定义starter

    四大核心 1.starter:jar包的引入,解决jar版本冲突问题 2.自动装配:简单配置甚至零配置即可运行项目 3.actuator:springboot程序监控器 4.CLI:命令行 start ...

  8. SpringBoot | 四大核心之actuator(程序监控器)

    Actuator 程序监控器 1. Actuator(程序监控器) 简介 2. 怎么使用 2.1 引入pom依赖 2.2 编写配置 2.3 启动程序 1. Actuator(程序监控器) 简介 act ...

  9. SpringBoot——四大核心之指标监控(actuator)

    1.写在前面 首先肯定要说一下SpringBoot的四大核心了: 自动装配:简单配置甚至零配置即可运行项目 起步依赖:场景启动器 Actuator:指标监控 命令行界面 :命令行 这篇文章呢,我来和大 ...

  10. 第二章 Spring Boot四大核心组件

    文章目录 前言 一.Spring Boot Starter 1.1 Starter的应用示例 1.2 Spring Boot之前的Thymeleaf和Mybatis应用 1.2.1 Thymeleaf ...

最新文章

  1. 调度器Quartz的简述与使用总结
  2. 【Uva 10934】Dropping water balloons
  3. CCF计算机职业资格认证2016-12-1
  4. [分布式训练] 单机多卡的正确打开方式:PyTorch
  5. 【MySQL】MySQL 8 ERROR 1193 (HY000): Unknown system variable ‘tx_isolation‘
  6. 解决AS3 Socket编程中最令人头疼的问题
  7. python不属于字符串的是_【python cookbook】python过滤字符串中不属于指定集合的字符...
  8. 计算机网络学习方法和书籍推荐
  9. 零知识证明在匿名投票中的应用
  10. 家庭医生后台管理系统高保真Axure原型模板
  11. 为什么要使用工作流引擎
  12. 【论文学习笔记-2】高分辨率3D深度重建
  13. JME-java开发3D游戏
  14. vue项目打包后出现页面布局异常、图片显示不出来等问题
  15. 正则表达式,去除空格标点下划线等
  16. superset设置起止时间为明天
  17. 解决Unable to find encoder for type stored in a Dataset问题
  18. 测试LINUX常用命令全集
  19. 学术会议日常英语交流_有效的日常会议的3个问题
  20. shell脚本之正则表达式与文本编辑器(一)

热门文章

  1. 利用iMazing将iOS设备的录音文件拷贝到电脑
  2. Redis 缓存 + Spring 的集成示例(转)
  3. POI 读取 Excel 文件(2003版本与2007版本的差异之处)
  4. python-各种tips
  5. Android点击通知栏信息后返回正在运行的程序,而不是一个新Activity
  6. 北京户口 - 百度百科
  7. 亿阳防火墙-命令行指令参考手册
  8. git 如何忽略掉指定目录
  9. poj Risk 1603 floyd基础题!!
  10. indesign教程,如何在对象周围环绕文本?