手把手帶你編寫屬于自己的 Starter
前面的文章阿粉給大家介紹了 ??SpringBoot 的自動裝配功能??,相信大家對自動裝配都有了很好的理解,那么今天阿粉通過一個示例來給大家演示一下如何編寫一個自己的 starter。
再編寫 starter 之前我們先了解一下什么是 starter,一個 starter 其實就是對一個功能的集成封裝,然后對外提供一個依賴,讓業(yè)務去使用,像我們熟悉的 Redis,mongo,mybatis 等。另外由于任何人都可以編寫自己的 starter,那么為了區(qū)分官方的 starter 和個人的 starter,通常在命名上面會有一個規(guī)范。所以 SpringBoot 官方提出,第三方在建立自己的 Starter 的時候命名規(guī)則統(tǒng)一用xxx-spring-boot-starter,而官方提供的 Starter 統(tǒng)一命名方式為spring-boot-starter-xxx。
通過我們前面的文章,我們知道自動裝配首先要有一個配置類,其次還要有 spring.factories 文件,所以這兩步是必不可少的。接下來我們就實操一下。
編寫配置類
編寫配置類首先要添加一個自動裝配的依賴,然后再編寫對應的配置類和業(yè)務實現(xiàn)類,在 pom 中添加如下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.7.1</version>
</dependency>
裝配類:
package com.example.hash.starter.config;
import com.example.hash.starter.service.MyHashTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass({MyHashTemplate.class})
@EnableConfigurationProperties(MyHashProperties.class)
public class MyHashAutoConfiguration {
@Autowired
MyHashProperties myHashProperties;
@Bean
@ConditionalOnMissingBean(MyHashTemplate.class)
public MyHashTemplate myJsonService() {
MyHashTemplate myHashTemplate = new MyHashTemplate();
myHashTemplate.setPrefix(myHashProperties.getHashPre());
return myHashTemplate;
}
}
屬性類:
package com.example.hash.starter.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "ziyou.hash")
public class MyHashProperties {
private String hashPre;
public String getHashPre() {
return hashPre;
}
public void setHashPre(String hashPre) {
this.hashPre = hashPre;
}
}
業(yè)務實現(xiàn)類:
package com.example.hash.starter.service;
import javax.xml.bind.DatatypeConverter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MyHashTemplate {
private String prefix;
public String myHash(String origin) {
if (null == origin || origin.length() == 0) {
return null;
}
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(origin.getBytes());
byte[] digest = md.digest();
return this.prefix + ":" + DatatypeConverter.printHexBinary(digest).toUpperCase();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
簡單說明一下上面三個類都是干什么的,MyHashTemplate 該類是實際業(yè)務需要注入的類,用來對入?yún)⑦M行 MD5 摘要,然后返回一個拼接了前綴的字符串給業(yè)務。這個前綴是通過 application.properties 中配置 ziyou.hash.hashPre=JavaGeekTech666 配置后傳遞過來的。MyHashProperties 是接受屬性值的類,MyHashAutoConfiguration 則是自動裝配類,這個類會根據(jù)條件進行 MyHashTemplate Bean 的初始化,并將前綴進行賦值。
增加配置文件
最后還需要在 resource 文件中編寫一個 META-INF/spring.factories 文件,內(nèi)容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.hash.starter.config.MyHashAutoConfiguration
前面的 Key 是固定寫法,后面的 value 就是配置類的全路徑引用地址。
在項目中使用
編寫完了 starter 過后,我們再創(chuàng)建一個新的 web 應用,在其中增加我們編寫的 starter 來驗證是否成功。第一步在 pom 文件中增加依賴
<dependency>
<groupId>com.starter.example</groupId>
<artifactId>myhash-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
package com.example.demo.controller;
import com.example.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping(value = "/hello")
public String hello(@RequestParam("name") String name) {
return helloService.sayHello(name);
}
}
package com.example.demo.service;
import com.example.hash.starter.service.MyHashTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
@Autowired
private MyHashTemplate myHashTemplate;
public String sayHello(String name) {
return myHashTemplate.myHash(name);
}
}
在 application.properties 文件中增加如下配置:
ziyou.hash.hashPre=JavaGeekTech
啟動項目,我們訪問地址 http://127.0.0.1:8080/hello?name=ziyou 可以看到效果如下:
至此可以看到,我們自己編寫的 starter 已經(jīng)成功生效了,只不過功能很簡單而已,我們完全可以根據(jù)自己需要的實際功能來實現(xiàn)一個復雜一點的 starter 來提供開箱即用的效果。
在一些大公司特別是一些中間件或者中臺團隊,很多時候都是提供各種工具的 starter 來給業(yè)務團隊使用的,畢竟很多重復的功能如果每個業(yè)務團隊都自己開發(fā)的話,完全是浪費資源,通過中臺團隊統(tǒng)一封裝 starter,讓各個業(yè)務團隊拿來就用快速搭建業(yè)務即可。