Spring Boot 中 Starter 是什么?它的核心规范有哪些?请说明如何自定义一个 Starter。

发布时间:2026/6/14 15:07:53
Spring Boot 中 Starter 是什么?它的核心规范有哪些?请说明如何自定义一个 Starter。 Spring Boot 中的 Starter在 Spring Boot 生态中Starter是一种简化依赖管理和项目配置的机制使开发者可以轻松地引入预配置好的功能模块。通过使用 Starter开发者可以在其项目中快速集成特定的功能而无需手动配置所有相关的依赖和设置。Starter通常以 Maven 依赖的形式存在命名以spring-boot-starter-开头后面跟着所需功能的描述。例如spring-boot-starter-web包含了开发 Web 应用所需的相关依赖和自动配置。Starter 的核心规范约定优于配置Spring Boot Starter 遵循“约定优于配置”的原则使开发者可以通过开箱即用的方式快速启动项目大部分配置都是默认好的。自动配置支持每个 Starter 通常会提供相应的自动配置类结合EnableAutoConfiguration注解自动装配相关的 Bean。包罗众多依赖Starter 通常会聚合多个相关依赖库简化 pom.xml 中的依赖管理。通过使用 Starter开发者只需引入一个依赖即可获得多个功能。条件配置Starter 在自动配置中提供了多种条件注解如ConditionalOnClass,ConditionalOnMissingBean等以根据环境条件灵活调整配置。自定义一个 Starter自定义一个 Starter 其实非常简单。以下步骤将引导你如何创建一个自定义的 Spring Boot Starter。1. 创建 Maven 项目首先创建一个 Maven 项目并在pom.xml中指定其类型为pom。设置项目的基本信息。projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.example/groupIdartifactIdmy-spring-boot-starter/artifactIdversion1.0.0/versionpackagingjar/packagingdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-autoconfigure/artifactId/dependency/dependencies/project2. 添加自动配置类接下来创建一个自动配置类用于定义 Starter 提供的功能。例如packagecom.example.mystarter;importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;ConfigurationpublicclassMyAutoConfiguration{BeanConditionalOnProperty(namemy.starter.enabled,havingValuetrue,matchIfMissingtrue)publicMyServicemyService(){returnnewMyService(Hello, Custom Starter!);}}3. 创建功能类定义一个简单的功能类MyService用于示范packagecom.example.mystarter;publicclassMyService{privateStringmessage;publicMyService(Stringmessage){this.messagemessage;}publicStringgetMessage(){returnmessage;}}4. 定义spring.factories在src/main/resources/META-INF目录下创建一个spring.factories文件以便 Spring Boot 可以识别你的自动配置类。org.springframework.boot.autoconfigure.EnableAutoConfiguration\ com.example.mystarter.MyAutoConfiguration5. 使用自定义 Starter最后在另一个 Spring Boot 项目中引入这个 Starter 依赖并使用你定义的服务dependencygroupIdcom.example/groupIdartifactIdmy-spring-boot-starter/artifactIdversion1.0.0/version/dependency然后在 Spring Boot 应用中你可以注入MyService并使用它packagecom.example.demo;importcom.example.mystarter.MyService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;SpringBootApplicationpublicclassDemoApplication{publicstaticvoidmain(String[]args){SpringApplication.run(DemoApplication.class,args);}RestControllerpublicclassMyController{AutowiredprivateMyServicemyService;GetMapping(/message)publicStringmessage(){returnmyService.getMessage();}}}最后小结下哈通过上述步骤你能够成功创建一个自定义的 Spring Boot Starter并利用 Spring Boot 提供的自动化特性使得其他项目可以轻松集成你的功能模块。自定义 Starter 是提升开发效率的有效方式它将复杂的依赖关系和配置集中管理以简化开发流程。

月新闻