项目结构
1.父级项目:my-springboot-starter
2.子module:my-spring-boot-starter
3.子module:my-spring-boot-starter-autoconfigure
4.starter引用autoconfigure
demo下载
下载地址
demo截图
注:spring.factories中不同key,spring boot对应生命周期时会调用对应类
demo部分示例
创建父pom
创建父项目my-springboot-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.3.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.my</groupId>
<artifactId>my-springboot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>my-springboot-starter</name>
<description>自定义spring boot starter</description>
<packaging>pom</packaging>
<modules>
<module>my-spring-boot-starter</module>
<module>my-spring-boot-starter-autoconfigure</module>
</modules>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
创建starter
在父项目中创建module,名字叫my-spring-boot-starter
此jar包内容为空,关键pom中引入autoconfigure
对外提供的时候只需引入starter包,自动引入autoconfigure
<?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>
<groupId>com.my</groupId>
<artifactId>my-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>my-spring-boot-starter-autoconfigure</name>
<description>自动配置类</description>
<parent>
<groupId>com.my</groupId>
<artifactId>my-springboot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>
创建autoconfigure
创建my-spring-boot-starter-autoconfigure项目,此项目实现自动注入
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my</groupId>
<artifactId>my-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>my-spring-boot-starter-autoconfigurer</name>
<description>自动配置类</description>
<parent>
<groupId>com.my</groupId>
<artifactId>my-springboot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>
创建 META-INF/spring.factories
设置自动注入配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.my.springboot.starter.autoconfigurer.HelloAutoConfitguration
创建HelloAutoConfitguration
package com.my.springboot.starter.autoconfigurer;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloAutoConfitguration {
public HelloAutoConfitguration(){
System.out.println("------EnableAutoConfiguration------");
}
}
maven install 成jar包
其他spring boot项目pom中引入starter则自动注入配置
<dependency>
<groupId>com.my</groupId>
<artifactId>my-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>