自定义Starter
我们分析完毕了源码以及自动装配的过程,我们可以尝试自定义一个启动器来玩玩!
官方命名:
前缀:spring-boot-starter-xxx
比如:spring-boot-starter-web…
编写启动器
1、在IDEA中新建一个空项目 spring-boot-starter-diy
2、新建一个普通Maven模块:kuang-spring-boot-starter
3、新建一个Springboot模块:kuang-spring-boot-starter-autoconfigure
4、点击apply即可,基本结构
5、在我们的 starter 中 导入 autoconfigure 的依赖
6、将 autoconfigure 项目下多余的文件都删掉,Pom中只留下一个 starter,这是所有的启动器基本配置!
7、我们编写一个自己的服务
编写HelloProperties 配置类
9、编写我们的自动配置类并注入bean,测试!
11、编写完成后,可以安装到maven仓库中!
新建项目测试我们自己写的启动器
1、新建一个SpringBoot 项目
2、导入我们自己写的启动器