SpringBoot四大核心组件,你知道几个?
先 透 露 一 下 , 四 大 组 件 分 别 是 : 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: 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.SqlSessionFactoryBea
n">
<!-- 注入数据库连接池 -->
<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 小结