什么是Starter
基于SpringBoot开发项目可以简化Spring应用的搭建以及开发过程,提高开发效率,这是由于其"约定大于配置"的策略及其自动装配的特点。
约定大于配置:并不是一个新套路,新技术,新思想,而是原来就一直存在的, Spring Boot 只不过是把它放大了,并真正的做到了约定优于配置。
Spring Boot 约定,当你导入
spring-boot-starter-web后,就约定了你是一个 web 开发环境,当你是一个 web 环境,就约定了你会使用 Spring MVC,至于其它的也约定你会需要,都给你默认导入进来,当你觉得不合适的时候,可以用更少的改动,满足你的需要。Spring 在推动“约定优于配置”这一设计理念,从 Spring 的注解版本(JDK5.0发布,采用元数据,引入注解的概念)就已经开始了。引入注解就是为了减少一些默认配置,引入注解也就代表着简化配置的开始,官方说基于 spring 的基础就是这个事实。
SpringBoot 约定以 starter 的形式减少依赖,于是相继推出了不少常用的 starter。
自动装配:指在使用某个组件或框架时需要引用其依赖、配置类、配置文件等工作时,SpringBoot 帮我们做了这些工作。
跟 Starter 有关系吗?答案是:有!Starter 就是自动装配的具体实现,其就是一个 maven 项目,对某个组件的依赖、配置进行管理。通过导入 “Starter” 模块更容易使用这个组件。
Starter的作用?
通过对比 mybatis-spring 和 mybatis-spring-boot-starter 代码示例,了解 Starter 的作用。
spring整合组件
spring 项目中使用 mybatis 。大概有以下几个步骤:
1、引入spring、mybatis、jdbc等相关依赖
2、创建mybatis-config.xml配置文件
2.1:声明数据源DataSource
2.2:声明SqlSessionFactoryBean
2.3:声明MapperScannerConfigurer
2.4:声明其他配置
3、编写XXXMapper.xml及XXMapper.java文件。
4、业务编码调用
相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<dependency>
<groupId>xxx</groupId>
<artifactId>xxxx</artifactId>
<version>xxx</version>
</dependency>
...
mybatis-config.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<context:property-placeholder location="classpath:jdbc.properties"/>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<mappers>
<package name="com.xxx.dao"/>
</mappers>
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:xxxx/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.dao"/>
</bean>
<bean class=".xxxxxx.xxx">
<!-- 指定SqlSessionFactory对象的名称 -->
<property name="sqlSessionFactoryBeanName" value="factory"/>
<!-- 指定基本包,dao接口所在的包名 -->
<property name="basePackage" value="com.xxx.dao"/>
</bean>
<bean class=".xxxxxx.xxx">
...
</bean>
</configuration>
业务编码调用
@Autowired
private XxxDao xxxDao;
xxxDao.insert(xx);
以上配置显得十分麻烦,如果稍不留神少了哪个配置或依赖,那只能排查问题
spring-boot整合组件
如果用基于 SpringBoot 开发,那 mybatis-spring-boot-starter 就可以帮助我们做这些事
SpringBoot 项目中如何使用 Mybatis 的。大概有以下几个步骤:
1、引入Mybatis-spring-boot-starter依赖。
2、application.properties文件中添加配置。
3、编写XXXMapper.xml及XXMapper.java文件。
4、业务编码调用
引入mybatis-spring-boot-starter 依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
application.properties 文件中添加相关配置
spring.datasource.username=x********
spring.datasource.password=********
spring.datasource.url=********
spring.datasource.driver-class-name=********
mybatis.mapper-locations=classpath:mapper/*.xml
编写 xxxMapper.xml 及 xxMapper.java 文件
@Mapper
public interface XXXMapper {
List<XXX> list(xxx);
}
业务编码调用
@Autowired
private XxxDao xxxDao;
xxxDao.insert(xx);
通过以上的代码比较可以明显的感觉到利用 Starter 后,我们编写的代码更少了
特别是 1、2 步骤,这就是 Starter 的作用。
mybatis-spring-boot-starter 帮助我们做了以下几件事:
1、整合了组件相关的依赖,使我们直接引入 mybatis-spring-boot-starter 依赖即可,也避免了版本冲突问题。
2、自动发现存在的 DataSource,做到自动配置。
3、帮我们创建并注册SqlSessionFactory、SqlSessionTemplate等,减少了配置类、配置项。
4、自动扫描映射器(Mapper),注入到 Spring Bean 中。
Starter原理
mybatis-spring-boot-starter 是如何做这些事的?
首先看 mybatis-spring-boot-starter 项目结构,其只有一个pom.xml文件,文件中已经帮我们引入相关依赖,跟上面 Spring 整合 Mybatis 的依赖是不是差不多。

其中有一个 mybatis-spring-boot-autoconfigure 依赖,我们看下其项目结构

其通过 SPI 机制引入了 MybatisAutoConfiguration 配置类,该类帮我们做了以下几件事:
1、发现存在的 DataSource 并注入配置。

2、注册 SqlSessionFactory、SqlSessionTemplate 到 Spring 容器中。

3、内部类 AutoConfiguredMapperScannerRegistrar 扫描存在 @Mapper 注解类转化为 BeanDefinition 并注册到 Spring 容器中。
Starter是SpringBoot自动装配的具体实现,通过引入starter模块,如mybatis-spring-boot-starter,可以简化Spring集成Mybatis的过程,自动处理依赖、配置和组件创建。它整合相关依赖,发现并配置DataSource,创建SqlSessionFactory等,减少了手动配置的工作量。Mybatis-spring-boot-starter通过SPI机制和配置类完成自动化配置。
1011

被折叠的 条评论
为什么被折叠?



