自定义starter
- 二方包 My-thread-pool-starter
- my-thread-pool-starter 整体架构
- 测试 MyTestAppApplication
- 测试工程 my-test-app 结构
- 测试项目的 pom.xml
二方包 My-thread-pool-starter
POM 文件
<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>
<groupId>com.example</groupId>
<artifactId>my-thread-pool-starter</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
my-thread-pool-starter 整体架构
│
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── threadpoolstarter
│ │ │ ├── ThreadPoolAutoConfiguration.java
│ │ │ ├── ThreadPoolProperties.java
│ │ │ └── ThreadPoolConfigurer.java
│ └── resources
│ └── META-INF
│ └── spring.factories
确保 spring.factories 文件正确配置并位于 src/main/resources/META-INF 目录下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.threadpoolstarter.ThreadPoolAutoConfiguration
打包和安装到本地仓库
mvn clean install
测试 MyTestAppApplication
在另一个 Spring Boot 应用中使用你的自定义 Starter,在pom.xml 中添加依赖项:
<dependency>
<groupId>com.example</groupId>
<artifactId>my-thread-pool-starter</artifactId>
<version>1.0.0</version>
</dependency>
然后在该应用的 application.properties 或 application.yml 文件中配置你的线程池属性:
threadpool.enabled=true
threadpool.corePoolSize=10
threadpool.maxPoolSize=50
threadpool.queueCapacity=100
threadpool.threadNamePrefix=CustomThreadPool-
测试工程 my-test-app 结构
│
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── mytestapp
│ │ │ └── MyTestAppApplication.java
│ └── main
│ └── resources
│ └── application.yml
测试项目的 pom.xml
<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>
<groupId>com.example</groupId>
<artifactId>my-test-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 添加自定义的Starter依赖 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>my-thread-pool-starter</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>