本文通過撰寫一個自定義starter來學習springboot的底層原理,幫助我們更好的使用springboot集成第三方插件
- 步驟一:創建專案
- 步驟二:添加依賴
- 步驟三:創建自動配置類
- 步驟四:創建屬性類
- 步驟五:創建服務類
- 步驟六:添加自動配置類到Springboot自動配置串列中
- 步驟七:打包并發布
- 步驟八:在其他專案中使用自定義starter
- 結論
步驟一:創建專案
世界上最偉大的成就,不是從不失敗,
而是每次失敗后,仍能振作起來,
首先,我們需要創建一個Springboot專案,洗掉啟動類,組態檔,添加META-INF檔案夾和spring.factories,最終專案結構如下
步驟二:添加依賴
在創建完專案后,我們需要在pom.xml檔案中添加以下三個依賴:
<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>
<version>2.7.0</version>
<optional>true</optional>
</dependency>
第一個依賴是Springboot的starter依賴,第二個依賴是Springboot的自動配置依賴,第三個是自動生成幫助檔案和元資料,以幫助IDE自動完成和引數提示,
步驟三:創建自動配置類
當你感到疲憊時,不要停下腳步,
因為你不知道,下一步可能就是成功,
自動配置類是整個自定義starter的核心部分,我們需要在自動配置類中完成以下任務:
- 使用
@Configuration注解將該類標記為配置類, - 使用
@EnableConfigurationProperties注解引入屬性配置類, - 使用
@Bean注解將自定義服務類注入到Spring容器中,
下面是一個示例代碼:
@Configuration
@EnableConfigurationProperties(YoulaiProperties.class)
public class YoulaiAutoConfiguration {
private final YoulaiProperties properties;
public YoulaiAutoConfiguration(YoulaiProperties properties) {
this.properties = properties;
}
@Bean
public YoulaiService myService() {
return new YoulaiService(properties.toString());
}
}
步驟四:創建屬性類
光陰似箭,日月如梭,生命短暫而珍貴,
讓我們在有生之年做自己想做的事情,
屬性類用來存盤自定義的配置屬性,我們需要在屬性類中完成以下任務:
- 使用
@ConfigurationProperties注解將該類標記為屬性類, - 使用
prefix屬性指定屬性名前綴, - 定義對應的屬性欄位,并提供
getter和setter方法,
下面是一個示例代碼:
@Component
@ConfigurationProperties(prefix = "youlai")
public class YoulaiProperties {
private String bio = "youlai-mall 是基于Spring Boot 2.7、Spring Cloud 2021 & Alibaba 2021、Vue3、Element-Plus、uni-app等全堆疊主流技術堆疊構建的開源商城專案,涉及 后端微服務、 前端管理、 微信小程式和 APP應用等多端的開發,";
private String serverPath="https://admin.youlai.tech/#/login?redirect=/dashboard";
private String apiPath="https://api.youlai.tech/doc.html#/home";
public String getBio() {
return bio;
}
public void setBio(String bio) {
this.bio = bio;
}
public String getServerPath() {
return serverPath;
}
public void setServerPath(String serverPath) {
this.serverPath = serverPath;
}
public String getApiPath() {
return apiPath;
}
public void setApiPath(String apiPath) {
this.apiPath = apiPath;
}
@Override
public String toString() {
return "YoulaiProperties{" +
"bio='" + bio + '\'' +
", serverPath='" + serverPath + '\'' +
", apiPath='" + apiPath + '\'' +
'}';
}
}
步驟五:創建服務類
服務類是自定義starter提供的服務,我們需要在服務類中定義服務方法,提供的服務,我這演示讀取組態檔
下面是示例代碼:
public class YoulaiService {
private String config;
public YoulaiService(String config) {
this.config = config;
}
public void doSomething() {
System.out.println("Config value is: " + config);
}
}
步驟六:添加自動配置類到Springboot自動配置串列中
為了讓Springboot能夠自動配置我們的starter,我們需要將自動配置類添加到META-INF/spring.factories檔案中,springboot啟動會讀取這個檔案,并將他注入到spring的容器中,如下所示:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.youlai.springbootstarter.YoulaiAutoConfiguration
除了這種自動裝配,還有一種被動裝配,由使用方決定是否需要裝配,這種只需要定義一個注解就好了
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(YoulaiAutoConfiguration.class)
public @interface EnableYoulai {
}
步驟七:打包并發布
完成以上步驟后,我們可以使用Idea將專案打包并發布到Maven倉庫中,我這里演示打包到本地
步驟八:在其他專案中使用自定義starter
成功不是偶然的,而是你日復一日努力的結果,
永遠不要放棄自己的夢想,
在其他Springboot專案中,我們只需要在pom.xml檔案中添加以下依賴即可:
<dependency>
<groupId>com.youlai-learing</groupId>
<artifactId>youlai-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
如果是被動裝配,則在啟動類加上開啟注解:
@SpringBootApplication
@EnableYoulai
public class SpringbootStarterConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootStarterConsumerApplication.class, args);
}
}
然后就可以在代碼中使用我們在自定義starter中提供的服務了,如下所示:
@Resource
private YoulaiService youlaiService;
@Test
void contextLoads() {
youlaiService.doSomething();
}
完整代碼地址:https://gitee.com/youlaiorg/youlai-learning.git
結論
通過本文的介紹,我們學習到如何撰寫Springboot自定義starter,并且清楚了springboot自動裝配的原理
把握當下,不留遺憾,珍惜身邊人,
感恩生命中的每一個瞬間,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/547273.html
標籤:其他
