前言
今天看到了一道面试题,如何实现一个自定义的 SpringBoot starter
,或者说,SpringBoot
如何加载我们自己的 jar
包?其实,仔细想想本质是考察 SpringBoot
的自动配置原理,了解了其自动配置原理即可实现 SpringBoot
自定义 starter
SpringBoot
自动配置原理:https://blog.csdn.net/weixin_38192427/article/details/115178359
SpringBoot
自定义 starter
创建多模块项目
SpringBoot
的官方 starter
命名方式为 spring-boot-starter-xxx
如 spring-boot-starter-quartz
,SpringBoot
官方建议第三方 starter
的命名方式一般为 xxx-spring-boot-starter
创建一个 custom-spring-boot-starter
项目,项目下面新建两个模块
hello-spring-boot-starter
(场景启动器,普通Maven
工程)hello-spring-boot-starter-autoconfigure
(自动配置包,需用到Spring Initializr
创建的Maven
工程)
项目编码
custom-spring-boot-starter
父模块 Maven
依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.9.RELEASE</version>
<relativePath></relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>