自定义 SpringBoot Starter
原理部分已有说明:SpringBoot 自定义 Starter 实现,直接上效果图:
1、Starter项目
1.1、Starter项目结构
- 创建一个Demo Project,模拟一个需要被封装的DemoModule模块,其中核心方法为exeModuleMethod
- 通过starter封装可以直接初始化DemoModule的实例到Spring容器
- 在Maven中引入starter,且在yml中配置相应到参数即可直接初始化DemoModule的实例
- 在应用中注入DemoModule即可使用其exeModuleMethod方法
E:.
│ .gitignore
│ pom.xml
└─src
└─main
├─java
│ └─com
│ └─tyron
│ └─mystarterdemo
│ │ MyStarterDemoApplication.java
│ │
│ ├─config
│ │ DemoAutoConfiguration.java
│ │ DemoProperties.java
│ │
│ └─module
│ DemoModule.java
│
└─resources
└─META-INF
spring.factories
1.2、pom.xml
<?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.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tyron</groupId>
<artifactId>MyStarterDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-springboot-starter</name>
<description>Demo project for Spring Boot starter</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
1.3、MyStarterDemoApplication.java
package com.tyron.mystarterdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyStarterDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MyStarterDemoApplication.class, args);
}
}
1.4、DemoModule.java
package com.tyron.mystarterdemo.module;
public class DemoModule {
private String version;
private String name;
public String exeModuleMethod() {
return "Demo module, name = " + name + ", version = " + version;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
1.5、DemoProperties.java
package com.tyron.mystarterdemo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "com.tyron")
public class DemoProperties {
private String version;
private String name;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
1.6、DemoAutoConfiguration.java
package com.tyron.mystarterdemo.config;
import com.tyron.mystarterdemo.module.DemoModule;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
public class DemoAutoConfiguration {
@Bean
public DemoModule demoModule(DemoProperties properties){
DemoModule demoModule = new DemoModule();
demoModule.setName(properties.getName());
demoModule.setVersion(properties.getVersion());
return demoModule;
}
}
1.7、spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tyron.mystarterdemo.config.DemoAutoConfiguration
1.8、打包
此为本地测试,如需项目使用 deploy 即可
mvn clean install
至此,Starter已开发完成,下面进行测试:
2、Starter-test 项目
2.1、pom.xml
引入上面打包好的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.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tyron</groupId>
<artifactId>starter-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>starter-test</name>
<description>starter-test</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.tyron</groupId>
<artifactId>demo-springboot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2、StarterTestApplication.java
package com.tyron.startertest;
import com.tyron.mystarterdemo.module.DemoModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class StarterTestApplication {
public static void main(String[] args) {
SpringApplication.run(StarterTestApplication.class, args);
}
@Autowired
private DemoModule demoModule;
@GetMapping("/test")
public String demo(){
return demoModule.exeModuleMethod();
}
}
2.3、application.yml
com:
tyron:
name: first starter
version: v1