SpringBoot四大核心组件,你知道几个?

沙海 2021年8月18日01:00:35Java评论37字数 6582阅读21分56秒阅读模式
摘要

SpringBoot四大核心组件,你知道几个? 戳一戳→ 程序员的成长之路

SpringBoot四大核心组件,你知道几个?

戳一戳→ 程序员的成长之路 文章源自JAVA秀-https://www.javaxiu.com/41312.html

SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

程序员的成长之路文章源自JAVA秀-https://www.javaxiu.com/41312.html

互联网/程序员/技术/资料共享 文章源自JAVA秀-https://www.javaxiu.com/41312.html

关注文章源自JAVA秀-https://www.javaxiu.com/41312.html

阅读本文大概需要 6 分钟。文章源自JAVA秀-https://www.javaxiu.com/41312.html

来自:blog.csdn.net/u011909918/文章源自JAVA秀-https://www.javaxiu.com/41312.html

前言

先透露一下,四大组件分别是:starter, autoconfigure, CLI 以及actuator。下面我们就来详细介绍一些他们有什么用。文章源自JAVA秀-https://www.javaxiu.com/41312.html

一、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文件中总会看到这两种依赖:文章源自JAVA秀-https://www.javaxiu.com/41312.html

spring-boot-starter-xxx 和 xxx-spring-boot-starter文章源自JAVA秀-https://www.javaxiu.com/41312.html

这就是spring boot的四大组件之一的starter。文章源自JAVA秀-https://www.javaxiu.com/41312.html

a、spring-boot-starter-thymeleaf文章源自JAVA秀-https://www.javaxiu.com/41312.html

SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

在这里插入图片描述文章源自JAVA秀-https://www.javaxiu.com/41312.html

b、mybatis-spring-boot-starter文章源自JAVA秀-https://www.javaxiu.com/41312.html

SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

在这里插入图片描述文章源自JAVA秀-https://www.javaxiu.com/41312.html

两种starter的区别就是 >>文章源自JAVA秀-https://www.javaxiu.com/41312.html

  • 官方提供的starter是这样的:spring-boot-starter-xxx文章源自JAVA秀-https://www.javaxiu.com/41312.html

  • 非官方的starter是这样的:xxx-spring-boot-starter文章源自JAVA秀-https://www.javaxiu.com/41312.html

其中xxx就是我们想要依赖的组件或者jar包。上例就是我们spring boot用来引入thymeleaf引擎和mybatis框架所配置的依赖。引入之后通过简单的约定配置就可以正常使用。比如:文章源自JAVA秀-https://www.javaxiu.com/41312.html

Thymeleaf引擎约定配置:文章源自JAVA秀-https://www.javaxiu.com/41312.html

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

Mybatis约定配置:文章源自JAVA秀-https://www.javaxiu.com/41312.html

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。文章源自JAVA秀-https://www.javaxiu.com/41312.html

1.2 Spring Boot之前的Thymeleaf和Mybatis应用

废话不多说,直接上代码:文章源自JAVA秀-https://www.javaxiu.com/41312.html

1.2.1 Thymeleaf配置

a. 添加对应依赖:文章源自JAVA秀-https://www.javaxiu.com/41312.html

<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配置文章源自JAVA秀-https://www.javaxiu.com/41312.html

<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. 添加对应依赖:文章源自JAVA秀-https://www.javaxiu.com/41312.html

 <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配置文章源自JAVA秀-https://www.javaxiu.com/41312.html

下面的第3, 4步骤就是Mybatis相关配置。第一步是引入资源配置。第二步是配置数据源文章源自JAVA秀-https://www.javaxiu.com/41312.html

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

1.2.3 小结

a、Starter 帮我们封装好了所有需要的依赖,避免我们自己添加导致的一些Jar包冲突或者缺少包的情况;文章源自JAVA秀-https://www.javaxiu.com/41312.html

b、Starter帮我们自动注入了需要的Bean实例到Spring 容器中,不需要我们手动配置(这个可以说是starter干的,实际上并不是,这里埋个坑,下面解答);文章源自JAVA秀-https://www.javaxiu.com/41312.html

所以: starter包的内容就是pom文件,就是一个依赖传递包。文章源自JAVA秀-https://www.javaxiu.com/41312.html

二、Spring Boot Autoconfigure

2.1 autoconfigure 简介

autoconfigure在我们的开发中并不会被感知,因为它是存在与我们的starter中的。所以我们的每个starter都是依赖autoconfigure的:文章源自JAVA秀-https://www.javaxiu.com/41312.html

SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

在这里插入图片描述文章源自JAVA秀-https://www.javaxiu.com/41312.html

当然我们也可以把autoconfig的内容直接放在starter包里边。文章源自JAVA秀-https://www.javaxiu.com/41312.html

a. spring-boot-autoconfigure:文章源自JAVA秀-https://www.javaxiu.com/41312.html

注意:这里有个点,就是官网提供的configure大多数在spring-boot-autoconfigure包里边,并没有单独创建新包。文章源自JAVA秀-https://www.javaxiu.com/41312.html

SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

在这里插入图片描述文章源自JAVA秀-https://www.javaxiu.com/41312.html

b、mybatis-spring-boot-autoconfigure文章源自JAVA秀-https://www.javaxiu.com/41312.html

SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

在这里插入图片描述文章源自JAVA秀-https://www.javaxiu.com/41312.html

2.2 小结

autoconfigure内容是配置Bean实例到Spring容器的实际代码实现包,然后提供给starter依赖。所以说1.2.3中的b项所说的配置Bean实例到Spring容器中实际是autoconfigure做的,因为是starter依赖它,所以也可以说是starter干的。文章源自JAVA秀-https://www.javaxiu.com/41312.html

所以:autocinfigure是starter体现出来的能力的代码实现文章源自JAVA秀-https://www.javaxiu.com/41312.html

三、Spring Boot CLI

Spring Boot CLI是一个命令行使用Spring Boot的客户端工具;主要功能如下:文章源自JAVA秀-https://www.javaxiu.com/41312.html

  • 运行groovy脚本 => 官网2.1文章源自JAVA秀-https://www.javaxiu.com/41312.html

  • 打包groovy文件到jar => 官网2.3文章源自JAVA秀-https://www.javaxiu.com/41312.html

  • 初始化Spring Boot项目 => 官网2.4文章源自JAVA秀-https://www.javaxiu.com/41312.html

  • 其他文章源自JAVA秀-https://www.javaxiu.com/41312.html

先上个官网文档:文章源自JAVA秀-https://www.javaxiu.com/41312.html

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-cli.html文章源自JAVA秀-https://www.javaxiu.com/41312.html

因为这个我们用的比较少,所以就不多赘述了。个人感觉比较流脾的功能就是命令行直接执行groovy脚本了。文章源自JAVA秀-https://www.javaxiu.com/41312.html

四、Spring Boot actuator

actuator是Spring Boot的监控插件,本身提供了很多接口可以获取当前项目的各项运行状态指标。文章源自JAVA秀-https://www.javaxiu.com/41312.html

官网文档:文章源自JAVA秀-https://www.javaxiu.com/41312.html

https://docs.spring.io/spring-boot/docs/2.4.0/reference/html/production-ready-features.html#production-ready文章源自JAVA秀-https://www.javaxiu.com/41312.html

名词解释:文章源自JAVA秀-https://www.javaxiu.com/41312.html

Endpoints: 需要监控的端点。参考官网第二节官网文档文章源自JAVA秀-https://www.javaxiu.com/41312.html

可用的端点:文章源自JAVA秀-https://www.javaxiu.com/41312.html

SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

在这里插入图片描述文章源自JAVA秀-https://www.javaxiu.com/41312.html

SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

在这里插入图片描述文章源自JAVA秀-https://www.javaxiu.com/41312.html

下方的是web工程的端点。文章源自JAVA秀-https://www.javaxiu.com/41312.html

使用方法如下:文章源自JAVA秀-https://www.javaxiu.com/41312.html

4.1 添加依赖

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

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

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

4.3 启动服务并验证

4.3.1 启动结果

SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

在这里插入图片描述文章源自JAVA秀-https://www.javaxiu.com/41312.html

4.3.2 查看各个监控信息

浏览器访问(查看监控信息地址):http://localhost:9500/actuator文章源自JAVA秀-https://www.javaxiu.com/41312.html

SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

在这里插入图片描述文章源自JAVA秀-https://www.javaxiu.com/41312.html

查看服务健康状态:文章源自JAVA秀-https://www.javaxiu.com/41312.html

SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

在这里插入图片描述文章源自JAVA秀-https://www.javaxiu.com/41312.html

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

总结

本章主要介绍了Spring Boot的四大组件的作用,其中主要是starter和autoconfigure,另外的CLI和actuator用的并不多,所以没有仔细介绍。文章源自JAVA秀-https://www.javaxiu.com/41312.html

<END>文章源自JAVA秀-https://www.javaxiu.com/41312.html

推荐阅读:文章源自JAVA秀-https://www.javaxiu.com/41312.html

字节教育大裁员:有员工赔了一套首付款!文章源自JAVA秀-https://www.javaxiu.com/41312.html

Java 中 long 是不是原子操作?文章源自JAVA秀-https://www.javaxiu.com/41312.html

最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。

文章源自JAVA秀-https://www.javaxiu.com/41312.html

获取方式:点个「在看」,点击上方小卡片,进入公众号后回复「面试题」领取,更多内容陆续奉上。文章源自JAVA秀-https://www.javaxiu.com/41312.html

朕已阅 SpringBoot四大核心组件,你知道几个?文章源自JAVA秀-https://www.javaxiu.com/41312.html

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定