2 步搞定springboot 自定義 starter
規範
一般情況下,我們會定義 2 個模塊,其中一個負責自動裝配,另一個負責依賴管理(啓動器)
命名規範爲:xxx-spring-boot-starter-autoconfigure、 xxx-spring-boot-starter
具體步驟
第1步 定義自動裝配器
1.1 新建模塊
根據命名規範爲:hello-springboot-starter-autoconfigure
1.2 配置maven
<?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.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.starter</groupId>
<artifactId>hello-springboot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入 spring-boot-starter,所有的 starter 都需要引入這個依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
1.3 定義裝配邏輯
這裏我們定義一個 sayhi 的組件,支持自定義前後綴,目的是讓使用者直接注入到容器中使用。
1.3.1 定義一個HelloService服務
public class HelloService {
private HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
/**
* say hi 方法
* @param name
* @return
*/
public String sayHi(String name){
return helloProperties.getPrefix() +"-"+ name +"-"+ helloProperties.getSuffix();
}
}
1.3.2 定義一個配置類,支持前後綴配置
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(value = "example.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
1.3.3 定義自動配置類
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.starter.HelloProperties;
@Configuration// 指定爲配置類
@ConditionalOnWebApplication // 指定條件:如果是 web 應用就生效
@EnableConfigurationProperties( HelloProperties.class ) // 啓動配置 properties
public class HelloServiceAutoconfiguration {
@Autowired
private HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
1.4 定義spring.factories
在resources
目錄下,新建 META-INF
目錄,在目錄新增一個名爲 spring.factories
的配置文件,內容如下:
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.starter.HelloServiceAutoconfiguration
其中配置格式爲 key,value 的格式,換行使用 \
,com.example.starter.HelloServiceAutoconfiguration
是我們自定義的自動配置類。
到此位置,自定義自動裝配器就搞定了。接下來,我們寫一下啓動器
第2步 定義啓動器
2.1 新建模塊
名爲:hello-springboot-starter
當然我這裏沒有按照規範命名,最好命名爲: hello-spring-boot-starter
2.2 配置 maven
這裏要引入自動裝配模塊
<?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>
<!--自定義starter啓動器-->
<groupId>com.example.starter</groupId>
<artifactId>hello-springboot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--引入自動裝配模塊-->
<dependency>
<groupId>com.example.starter</groupId>
<artifactId>hello-springboot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
好了,這個starter啓動器不需要任何代碼,只需要 pom 配置即可。
接下來,我們測試一下。
第3步 使用方式
3.1 配置maven
創建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.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.starter</groupId>
<artifactId>hello-springboot-starter-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入自定義 starter-->
<dependency>
<groupId>com.example.starter</groupId>
<artifactId>hello-springboot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 配置properties
當然這一步不是必要的,因爲我們的案例支持自定義前後綴,所以要測試一下。
在application.properties
配置文件配置我們定義的規則
example.hello.prefix=Welcome
example.hello.suffix=Hands up!!!
3.3 定義測試代碼
因爲是自動裝配,我們可以直接使用 @Autowired private HelloService helloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("hello/{name}")
public String hello(@PathVariable("name") String name){
return helloService.sayHi(name);
}
}
3.4 啓動測試
項目啓動後,訪問瀏覽器 http://localhost:8080/hello/Jack
源代碼見:https://gitee.com/yunnasheng/springboot-starter-example