什么是Starter?Starter其实就是我们经常在maven中的导入的各种模块,自定义Starter可以快速的满足开发的需求,并且可以重复使用无需粘贴代码,直接从maven仓库中导入即可。那么我们如何自定义一个Starter呢?这里我们以自动记录接口请求时间为例(自定义Starter的知识点只需看主要部分即可):
starter命令规则
这段话的大概意思就是,麻烦大家遵守这个命名规范:
Srping官方命名格式为:spring-boot-starter-{name}
非Spring官方建议命名格式:{name}-spring-boot-starter
项目演示
新建工程
首先新建一个maven 工程,名称定义为demo-spring-boot-starter
Pom引入依赖
<?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 http://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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.llc</groupId>
<artifactId>demo-spring-boot-starter</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- 提供了自动装配功能-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- 在编译时会自动收