1、创建starter项目
我电脑只有JDK 8,但是创建项目的时候最低只能选择17,后续创建完后去修改即可

2、项目结构
- 删除主启动类Application:Starter不需要启动类
- 删除配置文件application.properties:Starter不需要自己的配置文件
- 删除test里面的测试启动类
在resources下创建META-INF文件夹

3、修改JDK
修改成JDK8,如果你有更高的版本请切换

4、配置pom.xml
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <!-- 继承SpringBoot父项目 --> 6 <parent> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-parent</artifactId> 9 <version>2.6.3</version> <!-- 适配JDK 8 的版本 --> 10 <relativePath/> <!-- lookup parent from repository --> 11 </parent> 12 <!-- 项目基本信息 --> 13 <groupId>com.ckm</groupId> 14 <artifactId>starter</artifactId> 15 <version>0.0.1-SNAPSHOT</version> 16 <name>my-starter</name> 17 <description>my-starter</description> 18 <url/> 19 <licenses> 20 <license/> 21 </licenses> 22 <developers> 23 <developer/> 24 </developers> 25 <scm> 26 <connection/> 27 <developerConnection/> 28 <tag/> 29 <url/> 30 </scm> 31 32 <properties> 33 <java.version>8</java.version> <!-- JDK 8 --> 34 </properties> 35 <dependencies> 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-starter</artifactId> 39 </dependency> 40 41 <!-- 可选:配置元数据(后面文章会讲) --> 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-configuration-processor</artifactId> 45 <optional>true</optional> 46 </dependency> 47 48 <!-- 可选:Lombok,简化代码 --> 49 <dependency> 50 <groupId>org.projectlombok</groupId> 51 <artifactId>lombok</artifactId> 52 <optional>true</optional> 53 </dependency> 54 </dependencies> 55 56 <build> 57 <plugins> 58 <plugin> 59 <groupId>org.springframework.boot</groupId> 60 <artifactId>spring-boot-maven-plugin</artifactId> 61 <configuration> 62 <skip>true</skip> <!-- 打包时跳过主启动类,必须加这个不然打包会报错 --> 63 </configuration> 64 </plugin> 65 </plugins> 66 </build> 67 68</project> 69 70
注意这里的<optional>
1<!-- Starter项目 --> 2<!-- Starter项目 --> 3<dependency> 4 <groupId>org.projectlombok</groupId> 5 <artifactId>lombok</artifactId> 6 <optional>true</optional> ← 注意这里 7</dependency> 8
<optional>true</optional>的意思是Starter项目自己可以用Lombok,引用Starter项目的项目不会自动引入Lombok
5、创建业务类、自动配置类、配置属性类
5.1 业务类
1package com.ckm.starter; 2 3/** 4 * Hello服务类 5 * 提供简单的问候功能 6 */ 7public class HelloService { 8 9 private String prefix; // 前缀 10 private String suffix; // 后缀 11 12 public HelloService(String prefix, String suffix) { 13 this.prefix = prefix; 14 this.suffix = suffix; 15 } 16 17 /** 18 * 说Hello 19 * @param name 名字 20 * @return 问候语 21 */ 22 public String sayHello(String name) { 23 return prefix + " " + name + " " + suffix; 24 } 25} 26 27
5.2 自动配置类(核心)
1package com.ckm.starter; 2 3import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 4import org.springframework.boot.context.properties.EnableConfigurationProperties; 5import org.springframework.context.annotation.Bean; 6import org.springframework.context.annotation.Configuration; 7 8/** 9 * Hello自动配置类 10 * SpringBoot启动时会自动加载这个配置类 11 */ 12@Configuration // ← 标记为配置类 13@EnableConfigurationProperties(HelloProperties.class) // ← 启用配置属性 14public class HelloAutoConfiguration { 15 16 /** 17 * 注册HelloService到Spring容器 18 * @param properties 配置属性 19 * @return HelloService实例 20 */ 21 @Bean // ← 注册为Bean 22 @ConditionalOnMissingBean // ← 如果容器中没有HelloService才注册 23 public HelloService helloService(HelloProperties properties) { 24 return new HelloService(properties.getPrefix(), properties.getSuffix()); 25 } 26} 27 28
5.3 配置属性类
1package com.ckm.starter; 2 3import org.springframework.boot.context.properties.ConfigurationProperties; 4 5/** 6 * Hello配置属性类 7 * 对应配置文件中的 hello.* 配置 8 */ 9@ConfigurationProperties(prefix = "hello") 10public class HelloProperties { 11 12 /** 13 * 前缀,默认值:Hello 14 */ 15 private String prefix = "Hello"; 16 17 /** 18 * 后缀,默认值:! 19 */ 20 private String suffix = "!"; 21 22 // Getter 和 Setter 23 public String getPrefix() { 24 return prefix; 25 } 26 27 public void setPrefix(String prefix) { 28 this.prefix = prefix; 29 } 30 31 public String getSuffix() { 32 return suffix; 33 } 34 35 public void setSuffix(String suffix) { 36 this.suffix = suffix; 37 } 38} 39 40
三个类的关系:
1HelloProperties(配置) 2 ↓ 读取配置文件 3HelloAutoConfiguration(自动配置) 4 ↓ 创建并注册Bean 5HelloService(业务类) 6 ↓ 被注入到Spring容器 7使用方可以 @Autowired 使用 8
6、创建spring.factories
在resource的META-INF文件夹下创建spring.factories
文件内容:
1# Auto Configure 2org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 3com.ckm.starter.HelloAutoConfiguration 4
如果有多个可以使用逗号分割,如:
1org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2com.ckm.starter.HelloAutoConfiguration,\ 3com.ckm.starter.OtherAutoConfiguration 4
格式解读:
1org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 ↓ 3这是固定的key,SpringBoot会读取这个key 4 5com.ckm.starter.HelloAutoConfiguration 6 ↓ 7你的自动配置类的全限定名 8
SpringBoot约定必须是读取META-INF/spring.factories
1// SpringBoot源码(简化版) 2public class SpringFactoriesLoader { 3 public static final String FACTORIES_RESOURCE_LOCATION = 4 "META-INF/spring.factories"; // ← 看!写死的路径 5 6 public static List<String> loadFactoryNames() { 7 // 扫描所有jar包的 META-INF/spring.factories 8 // 读取配置 9 // 加载自动配置类 10 } 11} 12
7、打包
Maven三大打包命令
命令对比:
| 命令 | 全称 | 作用 | 结果 |
|---|---|---|---|
| package | 打包 | 编译+测试+打jar包 | jar包在target目录 |
| install | 安装 | package + 部署到本地仓库 | jar包在本地Maven仓库 |
| deploy | 部署 | install + 部署到远程仓库 | jar包在本地+远程仓库 |
我们这里测试直接使用install打包

8、其他项目使用
8.1 在其他项目pom.xml中引入
1<dependency> 2 <groupId>com.ckm</groupId> 3 <artifactId>starter</artifactId> 4 <version>0.0.1-SNAPSHOT</version> 5</dependency> 6

8.2 编写测试类
1package com.ckm.ball; 2 3import com.ckm.starter.HelloService; 4import org.junit.jupiter.api.Test; 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.boot.test.context.SpringBootTest; 7 8@SpringBootTest 9class AboutBallBootApplicationTests { 10 11 @Autowired 12 private HelloService helloService; 13 @Test 14 void contextLoads() { 15 System.out.println(helloService.sayHello("你好")); 16 } 17 18} 19 20

8.3 自定义配置hello属性值
1hello: 2 prefix: "世界" 3 suffix: "。" 4

本文参考:https://blog.csdn.net/2301%5F78967994/article/details/152665942
9、实现可插拔Starter
https://blog.csdn.net/2301%5F78967994/article/details/152666034
10、配置元数据让你的Starter拥有智能提示
https://blog.csdn.net/2301%5F78967994/article/details/152666090
随着版本的更新,现在不用自动配置元数据就能识别出来了
没有配置元数据:
