starter命名規範
spring提供的starter:
- spring-boot-starter-xxx-x.y.z.jar
- spring-boot-xxx-autoconfigure-x.y.z.jar
第三方提供的starter:
- xxx-spring-boot-starter-x.y.z.jar
動手製作自定義Spring-Boot-Starter的步驟
1、新建項目,引入依賴
引入spring-boot-starter、spring-boot-autoconfigure、第三方jar,如果需要生成配置元信息,加入spring-boot-configuration-processor
2、編寫功能實現類
3、編寫自動配置類
4、在resources/META-INF/spring.factories中配置自動裝配類
5、打包
這裏我們使用jsoup抓取網頁信息爲例,製作一個starter。
項目結構:
1、新建maven項目,引入依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath />
</parent>
<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.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
</dependencies>
2、編寫功能實現類
編寫實現抓取網頁內容的類
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class CrawlPageClient {
private String pageUrl;
private CrawlPageClient() {
}
public CrawlPageClient(String pageUrl) {
this.pageUrl = pageUrl;
}
public static CrawlPageClient of(String pageUrl) {
return new CrawlPageClient(pageUrl);
}
public String getPageUrl() {
return pageUrl;
}
public PageInfo getPageInfo() {
PageInfo pageInfo = new PageInfo();
try {
Document doc = Jsoup.connect(pageUrl).get();
pageInfo.setUrl(pageUrl);
pageInfo.setTitle(doc.title());
pageInfo.setContent(doc.html());
return pageInfo;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
網頁信息類
public class PageInfo {
private String url;
private String title;
private String content;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
3、編寫自動配置類
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties( {PageProperties.class })
public class CrawlAutoConfigure {
@Bean
public CrawlPageClient create(PageProperties pageProperties) {
return CrawlPageClient.of(pageProperties.getPageUrl());
}
}
頁面配置類,配置前綴:crawl
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = PageProperties.CRAWL_PREFIX)
public class PageProperties {
public static final String CRAWL_PREFIX = "crawl";
private String pageUrl = "http://www.baidu.com";
public String getPageUrl() {
return pageUrl;
}
public void setPageUrl(String pageUrl) {
this.pageUrl = pageUrl;
}
}
默認抓取百度的頁面內容
4、在resources/META-INF/spring.factories中配置自動裝配類
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.lee.springboot.CrawlAutoConfigure
5、打包
打成jar包: crawl-spring-boot-starter-1.0.jar,maven依賴信息:
<dependency>
<groupId>org.lee.springboot</groupId>
<artifactId>crawl-spring-boot-starter</artifactId>
<version>1.0</version>
</dependency>
測試自定義starter
最後,新建測試項目測試自定義的starter。
引入依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring-boot crawl -->
<dependency>
<groupId>org.lee.springboot</groupId>
<artifactId>crawl-spring-boot-starter</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
新建測試類:
import org.lee.springboot.CrawlPageClient;
import org.lee.springboot.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("crawl")
public class CrawlController {
@Autowired
private CrawlPageClient crawlPageClient;
@GetMapping("")
@ResponseBody
public PageInfo crawlPageInfo() {
StringBuilder pageBuilder = new StringBuilder();
pageBuilder.append("抓取到").append(crawlPageClient.getPageUrl())
.append("網頁的信息<br/>").append(crawlPageClient.getPageInfo());
System.out.println(crawlPageClient.getPageInfo());
return crawlPageClient.getPageInfo();
}
}
運行程序,瀏覽器輸入:http://127.0.0.1:8080/crawl
默認看到的是百度網頁的信息:
我們在application.properties配置中加入以下配置:
crawl.page-url=https://www.csdn.net
抓取網頁改成csdn的,再次運行程序,訪問內容如下:
總結
本文簡單介紹如何開發自定義的Spring-Boot-Starter,通過添加依賴自動完成功能導入,使用起來很方便,大家也可以自己動手實現自定義的starter。