【SpringBoot】【Starter】----SpringBoot Starter 全解:原理、场景、实战自定义 Starter 文章目录一、什么是 SpringBoot Starter1. 定义2. 核心两大组成二、Starter 核心作用 ✨三、适用业务场景 四、Starter 底层工作原理 4.1 整体流程Mermaid流程图带配色4.2 关键核心注解说明4.3 文件机制五、实战1使用官方StarterWeb示例 5.1 Maven引入依赖 pom.xml5.2 application.yml 简单配置5.3 启动类代码5.4 启动访问命令1. Maven启动项目2. curl 测试接口六、实战2自定义业务Starter企业通用工具封装 ️6.1 工程结构双模块标准6.2 步骤1编写配置属性类6.3 步骤2业务工具类6.4 步骤3自动配置类6.5 步骤4注册自动配置清单6.6 步骤5聚合模块pom仅依赖autoconfigure6.7 项目引入自定义Starter使用6.8 测试使用七、官方常用Starter清单 八、开发总结 一、什么是SpringBoot Starter1. 定义Starter是SpringBoot提供的一站式依赖封装组件是官方简化开发的核心设计。它把一类功能所需的依赖包、自动配置类统一打包开发者无需手动管理版本、导入多个jar。2. 核心两大组成xxx-spring-boot-starter依赖聚合模块仅管理所有相关jar依赖xxx-spring-boot-starter-autoconfigure自动配置核心存放配置类、条件注解二、Starter 核心作用 ✨版本统一管理内置SpringBoot父工程统一版本杜绝版本冲突零繁杂依赖导入引入一个starter替代十多个零散jar自动装配依据application.yml配置自动创建Bean减少XML/Java配置开箱即用引入依赖后简单配置即可使用无需手动编写初始化代码规范统一官方/第三方遵循相同starter开发规范学习成本低三、适用业务场景 基础web开发spring-boot-starter-web接口、前后端交互数据库操作spring-boot-starter-mybatis、spring-boot-starter-jdbc缓存中间件spring-boot-starter-data-redis消息队列spring-boot-starter-amqpRabbitMQ测试场景spring-boot-starter-test单元测试、集成测试自定义业务组件封装公司内部工具、通用SDK自研自定义Starter四、Starter 底层工作原理 4.1 整体流程Mermaid流程图带配色满足条件不满足启动类 SpringBootApplicationEnableAutoConfiguration读取 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports加载所有自动配置类 AutoConfiguration条件注解校验 ConditionalOnClass/ConditionalOnProperty自动注册Bean到Spring容器跳过当前配置类读取 application.yml 配置绑定到 ConfigurationProperties4.2 关键核心注解说明SpringBootApplication组合注解包含自动配置开关EnableAutoConfiguration开启自动装配核心入口ConditionalOnClass类存在时才生效配置ConfigurationProperties绑定yml配置文件参数AutoConfiguration新版SpringBoot自动配置标记注解4.3 文件机制自动配置清单文件路径META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件内写入自定义配置类全限定名Spring启动时自动扫描加载。五、实战1使用官方StarterWeb示例 5.1 Maven引入依赖 pom.xml!-- SpringBoot Web Starter --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency5.2 application.yml 简单配置server:port:8080servlet:context-path:/demo5.3 启动类代码importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;SpringBootApplicationRestControllerpublicclassWebDemoApplication{publicstaticvoidmain(String[]args){SpringApplication.run(WebDemoApplication.class,args);}GetMapping(/hello)publicStringhello(){returnSpringBoot Starter Web 测试成功~;}}5.4 启动访问命令1. Maven启动项目mvn spring-boot:run2. curl 测试接口curlhttp://127.0.0.1:8080/demo/hello六、实战2自定义业务Starter企业通用工具封装 ️6.1 工程结构双模块标准custom-log-starter ├── custom-log-spring-boot-starter # 依赖聚合模块 └── custom-log-spring-boot-autoconfigure # 自动配置核心模块6.2 步骤1编写配置属性类importorg.springframework.boot.context.properties.ConfigurationProperties;ConfigurationProperties(prefixcustom.log)publicclassLogProperties{// 是否开启日志打印privatebooleanenabletrue;// 日志输出前缀privateStringprefix自定义日志;// getter/setter}6.3 步骤2业务工具类publicclassCustomLogUtil{privateLogPropertiesproperties;publicCustomLogUtil(LogPropertiesproperties){this.propertiesproperties;}publicvoidprintLog(Stringmsg){if(properties.isEnable()){System.out.println(properties.getPrefix()msg);}}}6.4 步骤3自动配置类importorg.springframework.boot.autoconfigure.condition.ConditionalOnClass;importorg.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;importorg.springframework.boot.context.properties.EnableConfigurationProperties;importorg.springframework.context.annotation.Bean;ConditionalOnClass(CustomLogUtil.class)EnableConfigurationProperties(LogProperties.class)publicclassCustomLogAutoConfiguration{BeanConditionalOnMissingBeanpublicCustomLogUtilcustomLogUtil(LogPropertieslogProperties){returnnewCustomLogUtil(logProperties);}}6.5 步骤4注册自动配置清单在autoconfigure模块新建文件resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件内写入配置类全类名com.example.log.config.CustomLogAutoConfiguration6.6 步骤5聚合模块pom仅依赖autoconfiguredependenciesdependencygroupIdcom.example/groupIdartifactIdcustom-log-spring-boot-autoconfigure/artifactIdversion1.0.0/version/dependency/dependencies6.7 项目引入自定义Starter使用pom.xmldependencygroupIdcom.example/groupIdartifactIdcustom-log-spring-boot-starter/artifactIdversion1.0.0/version/dependencyapplication.yml 配置custom:log:enable:trueprefix:项目统一日志6.8 测试使用importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;importcom.example.log.CustomLogUtil;importjavax.annotation.Resource;RestControllerpublicclassLogTestController{ResourceprivateCustomLogUtilcustomLogUtil;GetMapping(/log)publicStringtestLog(){customLogUtil.printLog(接口调用日志);return日志打印完成;}}七、官方常用Starter清单 Starter名称用途spring-boot-starter-webWeb接口开发spring-boot-starter-test单元测试spring-boot-starter-data-redisRedis缓存操作spring-boot-starter-mybatisMybatis数据库spring-boot-starter-amqpRabbitMQ消息队列spring-boot-starter-mail邮件发送八、开发总结 官方Starter优先使用版本兼容、稳定无坑自定义Starter适合封装团队通用工具、SDK复用代码核心本质自动配置文件 条件注解 配置属性绑定开发规范必须拆分autoconfigure和starter两个模块遵循SpringBoot标准规范