这个类就是一个Configuration
(配置类),它里面定义很多bean,其中最重要的就是SqlSessionFactory
的bean实例,该实例是Mybatis
的核心功能,用它创建SqlSession
, 对数据库进行CRUD操作 。
除此之外,MybatisAutoConfiguration
类还包含了:
- @ConditionalOnClass 配置了只有包含SqlSessionFactory.class和SqlSessionFactoryBean.class,该配置类才生效 。
- @ConditionalOnBean 配置了只有包含dataSource实例时,该配置类才生效 。
- @EnableConfigurationProperties 该注解会自动填充MybatisProperties实例中的属性 。
- AutoConfigureAfter 配置了该配置类在DataSourceAutoConfiguration类之后自动配置 。
接下来,重点看看
spring.factories
文件有啥内容:org.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
里面只有一行配置 , 即key
为EnableAutoConfiguration
,value
为MybatisAutoConfiguration
。好了,介绍了这么多东西,现在我们来总结一下,
starter几个要素如下图所示:

文章插图
那么,编写starter需要哪些步骤?
- 1.需要定义一个名称为xxx-spring-boot-starter的空项目 , 里面不包含任何代码 , 可以有pom.xml和pom.properties文件 。
- 2.pom.xml文件中包含了名称为xxx-spring-boot-autoconfigure的项目 。
- 3.xxx-spring-boot-autoconfigure项目中包含了名称为xxxAutoConfiguration的类,该类可以定义一些bean实例 。当然,Configuration类上可以打一些如:ConditionalOnClass、ConditionalOnBean、EnableConfigurationProperties等注解 。
- 4.需要在spring.factories文件中增加key为EnableAutoConfiguration,value为xxxAutoConfiguration 。
3 如何定义自己的starter?3.1 先创建一个空项目该项目名称为id-generate-starter,注意为了方便我把项目重命名了,原本应该是叫id-generate-spring-boot-starter的 , 如下图所示:

文章插图
pom.xml文件定义如下:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><version>1.3.1</version><groupId>com.sue</groupId><artifactId>id-generate-spring-boot-starter</artifactId><name>id-generate-spring-boot-starter</name><dependencies><dependency><groupId>com.sue</groupId><artifactId>id-generate-spring-boot-autoconfigure</artifactId><version>1.3.1</version></dependency></dependencies></project>
我们看到,它只引入了id-generate-spring-boot-autoconfigure 。当然如果有需要这里还可以引入多个autoconfigure或者多个其他jar包或者 。3.2 创建id-generate-autoconfigure同样为了方便我把项目重命名了,原本是叫id-generate-spring-boot-autoconfigure,如下图所示:

文章插图
该项目当中包含:pom.xml、spring.factories、IdGenerateAutoConfiguration、IdGenerateService 和 IdProperties 这5个关键文件,下面我们逐一看看 。
先从pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.RELEASE</version></parent><modelVersion>4.0.0</modelVersion><version>1.3.1</version><groupId>com.sue</groupId><artifactId>id-generate-spring-boot-autoconfigure</artifactId><name>id-generate-spring-boot-autoconfigure</name><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build></project>
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 现代诗的格式 现代诗格式模板
- 送老婆礼物前十件排名,每个都是女人无法拒绝的,你知道吗
- 怎么看手机指南针是正确的(手机指南针红色指向什么方向)
- 一加手机怎么样如何向只认牌子的妈妈推荐一加手机
- 一加手机怎么样各方面如何(国内的一加手机怎么样)
- P7476 苦涩 题解
- Springboot 之 HandlerMethodReturnValueHandler 运用
- ERP 系统的核心是什么?有什么作用?
- NIKKE胜利女神红辣椒的委托怎么做
- 申请qq号是怎么申请的(怎么申请两个qq号)