starter 是 SpringBoot 中一种非常重要的机制,它可以繁杂的配置统一集成到 starter 中,我们只需要通过 maven 将 starter 依赖引入到项目中,SpringBoot 就能自动扫描并加载相应的默认配置。starter 的出现让开发人员从繁琐的框架配置中解放出来,将更多的精力专注于业务逻辑的开发,极大的提高了开发效率。
在一些特殊情况下,我们也可以将一些通用功能封装成自定义的 starter 进行使用,本节我们将为您详细介绍如何自定义 starter。
SpringBoot 提供的 starter 以 spring-boot-starter-xxx 的形式命名。为了与 SpringBoot 生态提供的 starter 进行区分,官方建议第三方开发者或技术(例如 Druid、Mybatis 等等)厂商自定义的 starter 使用 xxx-spring-boot-starter 的形式命名,例如 mybatis-spring-boot-starter、druid-spring-boot-starter 等等。
Spring Boot 官方建议我们在自定义 starter 时,创建两个 Module :autoConfigure Module 和 starter Module,其中 starter Module 依赖于 autoConfigure Module。
当然,这只是 Spring Boot 官方的建议,并不是硬性规定,若不需要自动配置代码和依赖项目分离,我们也可以将它们组合到同一个 Module 里。
自定义 starter 可以分为以下 7 步:
1、使用 IntelliJ IDEA 创建一个空项目(Empty Project),如下图。
2、 在 Project Structure 界面,点击左上角的“+”按钮,选择“New Module”,
新建一个名为 bianchengbang-hello-spring-boot-starter 的 Maven Module 和一个名为 bianchengbang-helllo-spring-boot-starter-autoconfiguration 的 Spring Boot Module,如下图。
在 bianchengbang-hello-spring-boot-starter 的 pom.xml 中添加以下代码,将 bianchengbang-helllo-spring-boot-starter-autoconfiguration 作为其依赖项。
<!--添加自动配置模块为其依赖--><dependencies><dependency><groupId>net.biacheng.www</groupId><artifactId>bianchengbang-helllo-spring-boot-starter-autoconfiguration</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
在 bianchengbang-helllo-spring-boot-starter-autoconfiguration 的 net.biacheng.www.properties 包中,创建一个实体类:HelloProperties,通过它来映射配置信息,其代码如下。
package net.biacheng.www.properties;import org.springframework.boot.context.properties.ConfigurationProperties;/*** 实体类,用来映射配置信息*/@ConfigurationProperties("net.biancheng.www.hello")public class HelloProperties {private String prefix;private String suffix;public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;}}
HelloProperties 类上使用了 @ConfigurationProperties(“net.biancheng.www.hello”),其含义是将该类中的属性与配置文件中的以 net.biancheng.www.hello 开头的配置进行绑定。
package net.biacheng.www.service;import net.biacheng.www.properties.HelloProperties;import org.springframework.beans.factory.annotation.Autowired;/*** servcie 类,供外部调用*/public class HelloService {@AutowiredHelloProperties helloProperties;public String sayHello(String userName) {return helloProperties.getPrefix() + userName + helloProperties.getSuffix();}}
在 bianchengbang-helllo-spring-boot-starter-autoconfiguration 的 net.biacheng.www.autoConfiguration 中,创建一个配置类:HelloAutoConfiguration,其代码如下。
package net.biacheng.www.autoConfiguration;import net.biacheng.www.properties.HelloProperties;import net.biacheng.www.service.HelloService;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@EnableConfigurationProperties(HelloProperties.class) //启用 HelloProperties,并默认将它添加到容器中public class HelloAutoConfiguration {@ConditionalOnMissingBean(HelloService.class) //当容器中没有 HelloService 时生效@Beanpublic HelloService helloService() {HelloService helloService = new HelloService();return helloService;}}
HelloAutoConfiguration 使用了以下 4 个注解:
由于 Spring Boot 的自动配置是基于 Spring Factories 机制实现的,因此我们自定义 starter 时,同样需要在项目类路径下创建一个 spring.factories 文件。
在 bianchengbang-helllo-spring-boot-starter-autoconfiguration 的类路径下(resources )中创建一个 META-INF 文件夹,并在 META-INF 文件夹中创建一个 spring.factories 文件,如下图。
将 Spring Boot 的 EnableAutoConfiguration 接口与自定义 starter 的自动配置类 HelloAutoConfiguration 组成一组键值对添加到 spring.factories 文件中,以方便 Spring Boot 在启动时,获取到自定义 starter 的自动配置,代码如下。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\net.biacheng.www.autoConfiguration.HelloAutoConfiguration
Spring Factories 机制是 Spring Boot 中的一种服务发现机制,这种机制与 Java SPI 机制十分相似。Spring Boot 会自动扫描所有 Jar 包类路径下 META-INF/spring.factories 文件,并读取其中的内容,进行实例化,这种机制也是 Spring Boot Starter 的基础。
接下来,我们需要对自定义 starter 进行构建,并将它安装到本地仓库或远程仓库中,供其他项目使用。由于我们是在本地的项目中引用和测试,因此只需要使用 install 命令安装到本地仓库即可。
由于 starter Module 是依赖于 autoConfigure Module 的,因此我们需要先对 autoConfigure Module 进行构建。
在 bianchengbang-helllo-spring-boot-starter-autoconfiguration 的 pom.xml 中执行以下 mvn 命令,对它进行构建。
mvn clean install
命令执行结果如下。
[INFO] Scanning for projects...[INFO][INFO] --< net.biacheng.www:bianchengbang-helllo-spring-boot-starter-autoconfiguration >--[INFO] Building bianchengbang-helllo-spring-boot-starter-autoconfiguration 0.0.1-SNAPSHOT[INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Deleting D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\target[INFO][INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] Using 'UTF-8' encoding to copy filtered properties files.[INFO] Copying 0 resource[INFO] Copying 1 resource[INFO][INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Changes detected - recompiling the module![INFO] Compiling 3 source files to D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\target\classes[INFO][INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] Using 'UTF-8' encoding to copy filtered properties files.[INFO] skip non existing resourceDirectory D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\src\test\resources[INFO][INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] No sources to compile[INFO][INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] No tests to run.[INFO][INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Building jar: D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\target\bianchengbang-helllo-spring-boot-starter-autoconfiguration-0.0.1-SNAPSHOT.jar[INFO][INFO] --- maven-install-plugin:2.5.2:install (default-install) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Installing D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\target\bianchengbang-helllo-spring-boot-starter-autoconfiguration-0.0.1-SNAPSHOT.jar to D:\myRepository\repository\net\biacheng\www\bianchengbang-helllo-spring-boot-starter-autoconfiguration\0.0.1-SNAPSHOT\bianchengbang-helllo-spring-boot-starter-autoconfiguration-0.0.1-SNAPSHOT.jar[INFO] Installing D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\pom.xml to D:\myRepository\repository\net\biacheng\www\bianchengbang-helllo-spring-boot-starter-autoconfiguration\0.0.1-SNAPSHOT\bianchengbang-helllo-spring-boot-starter-autoconfiguration-0.0.1-SNAPSHOT.pom[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 2.285 s[INFO] Finished at: 2021-07-05T09:59:11+08:00[INFO] ------------------------------------------------------------------------
在 autoConfigure Module 构建完成后,接下来我们就可以对 starter Module 进行构建,构建完成后,其他 Spring Boot 项目便可以引用该自定义 starter 了。
在 bianchengbang-hello-spring-boot-starter 的 pom.xml 中执行以下 mvn 命令,对它进行构建。
mvn clean install
命令执行结果如下。
[INFO] Scanning for projects...[INFO][INFO] --< net.biacheng.www:bianchengbang-helllo-spring-boot-starter-autoconfiguration >--[INFO] Building bianchengbang-helllo-spring-boot-starter-autoconfiguration 0.0.1-SNAPSHOT[INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Deleting D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\target[INFO][INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] Using 'UTF-8' encoding to copy filtered properties files.[INFO] Copying 0 resource[INFO] Copying 1 resource[INFO][INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Changes detected - recompiling the module![INFO] Compiling 3 source files to D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\target\classes[INFO][INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] Using 'UTF-8' encoding to copy filtered properties files.[INFO] skip non existing resourceDirectory D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\src\test\resources[INFO][INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] No sources to compile[INFO][INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] No tests to run.[INFO][INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Building jar: D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\target\bianchengbang-helllo-spring-boot-starter-autoconfiguration-0.0.1-SNAPSHOT.jar[INFO][INFO] --- maven-install-plugin:2.5.2:install (default-install) @ bianchengbang-helllo-spring-boot-starter-autoconfiguration ---[INFO] Installing D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\target\bianchengbang-helllo-spring-boot-starter-autoconfiguration-0.0.1-SNAPSHOT.jar to D:\myRepository\repository\net\biacheng\www\bianchengbang-helllo-spring-boot-starter-autoconfiguration\0.0.1-SNAPSHOT\bianchengbang-helllo-spring-boot-starter-autoconfiguration-0.0.1-SNAPSHOT.jar[INFO] Installing D:\IDEA\demo\spring-boot-my-starter\bianchengbang-helllo-spring-boot-starter-autoconfiguration\pom.xml to D:\myRepository\repository\net\biacheng\www\bianchengbang-helllo-spring-boot-starter-autoconfiguration\0.0.1-SNAPSHOT\bianchengbang-helllo-spring-boot-starter-autoconfiguration-0.0.1-SNAPSHOT.pom[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 2.257 s[INFO] Finished at: 2021-07-05T10:06:44+08:00[INFO] ------------------------------------------------------------------------
当引用自定义 starter 的项目不在本地时,我们需要使用 mvn 命令“mvn clean deploy”将自定义 starter 部署到远程仓库中。
1、创建一个名为 test-my-starter 的 Spring Boot 项目,并在其 pom.xml 中引入依赖 bianchengbang-hello-spring-boot-starter(自定义 starter),代码如下。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.2</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>net.biacheng.www</groupId><artifactId>test-my-starter</artifactId><version>0.0.1-SNAPSHOT</version><name>test-my-starter</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!--引入 web 功能的 starter--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--引入测试 starter--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--引入自定义 starter --><dependency><groupId>net.biancheng.www</groupId><artifactId>bianchengbang-hello-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2、在 Spring Boot 配置文件 application.properties 中,添加以下属性配置。
#自定义 starter prefix 属性net.biancheng.www.hello.prefix=你好:#自定义 starter suffix 属性net.biancheng.www.hello.suffix=,欢迎您来到编程帮!
3、在 net.biancheng.www.controller 中创建一个控制器类 HelloController,代码如下。
package net.biacheng.www.controller;import net.biacheng.www.service.HelloService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class HelloController {//自动装配自定义 starter 的 service@AutowiredHelloService helloService;@ResponseBody@GetMapping("/hello")public String SayHello(String name) {return helloService.sayHello(name);}}
4、启动 Spring Boot,使用浏览器访问“http://localhost:8080/hello?name=小明”,结果如下图。
图2:访问结果
可以看到,我们自定义的 starter 已经生效。
加入
QQ群:722461036
微信群:
一起督促、学习、练习、温习、复习 ~ ~ ~
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号