当前位置:首页 > 机动车 >

springboot的四大核心(springboot的启动流程)

来源:原点资讯(www.yd166.com)时间:2023-11-09 18:43:50作者:YD166手机阅读>>

前言

先透露一下,四大组件分别是:starter, autoconfigure, CLI 以及actuator。

下面我们就来详细介绍一些他们有什么用。

一、Spring Boot Starter1.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 基础就不介绍了,推荐下这个实战教程:

https://github.com/javastacks/spring-boot-best-practice

在我们的Spring Boot项目种的POM文件中总会看到这两种依赖:

spring-boot-starter-xxx 和 xxx-spring-boot-starter。

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

a、spring-boot-starter-thymeleaf

springboot的四大核心,springboot的启动流程(1)

b、mybatis-spring-boot-starter

springboot的四大核心,springboot的启动流程(2)

两种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。

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/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包冲突或者缺少包的情况;

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

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

二、Spring Boot Autoconfigure2.1 autoconfigure 简介

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

springboot的四大核心,springboot的启动流程(3)

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

a. spring-boot-autoconfigure:

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

springboot的四大核心,springboot的启动流程(4)

首页 123下一页

栏目热文

主板电池拔掉后进不去系统了(主板电池没电能开机无法进入系统)

主板电池拔掉后进不去系统了(主板电池没电能开机无法进入系统)

“昨天公司遭遇突然的停电,导致无法继续工作,只得提前下班回家。今天回到办公室,电脑却陷入了启动问题。我试图多次重启,希望...

2023-11-09 18:37:10查看全文 >>

换了主板电池后无法进入系统

换了主板电池后无法进入系统

聊聊技嘉B460M DS3H V2主板返厂维修后进不了系统及无盘win7的处理过程技嘉B460M DS3H V2需要处理...

2023-11-09 19:03:52查看全文 >>

主板拔了电池后进不了系统了(电脑放了一年开不了机)

主板拔了电池后进不了系统了(电脑放了一年开不了机)

笔记本内存条DDR3-PC3L(L代表低电压的意思)台式机三代内存条,几代可以看标签或卡口位置现在使用电脑的朋友多了,以...

2023-11-09 18:57:57查看全文 >>

更换主板电池后电脑启动不了(更换电脑主板电池注意事项)

更换主板电池后电脑启动不了(更换电脑主板电池注意事项)

电脑DIY避坑,我干过的蠢事 篇二 开机没反应,折腾换件1小时才发现原因很简单身为DIY爱好者,写过的电脑相关文章也有几...

2023-11-09 19:12:41查看全文 >>

苹果手机用安卓充电头不能充电(苹果手机可用安卓充电头吗)

苹果手机用安卓充电头不能充电(苹果手机可用安卓充电头吗)

随着iPhone15系列的正式发布,Lightning数据接口也算是正式的在苹果系产品中寿终正寝。苹果在10年前发布Li...

2023-11-09 18:39:13查看全文 >>

springboot启动过程(springboot是什么)

springboot启动过程(springboot是什么)

现在Java的项目开发中,几乎都会引入Spring框架,甚至有人说java开发现在就是在面向Spring编程。基于Spr...

2023-11-09 19:11:51查看全文 >>

springboot全局异常处理(spring boot异常处理流程)

springboot全局异常处理(spring boot异常处理流程)

Spring Boot提供了一种简单且灵活的方式来处理应用程序中的异常,即全局异常处理。全局异常处理允许我们定义一个统一...

2023-11-09 18:37:04查看全文 >>

springboot项目实战视频(spring项目实例)

springboot项目实战视频(spring项目实例)

Spring Boot 3是一个非常令人期待的版本,将进一步扩大Spring Boot框架在应用程序开发领域的影响力,并...

2023-11-09 18:58:14查看全文 >>

springboot优缺点(springboot可以开发安卓吗)

springboot优缺点(springboot可以开发安卓吗)

SpringBoot核心功能2.1、独立运行Spring项目Spring boot 可以以jar包形式独立运行,运行一个...

2023-11-09 19:10:21查看全文 >>

springboot启动过程图解(SpringBoot启动流程)

springboot启动过程图解(SpringBoot启动流程)

springboot启动过程中会用到事件发布机制。这里先事件发布说明一下。Spring中的事件发布机制。网上有一篇文章介...

2023-11-09 18:32:06查看全文 >>

文档排行