什么是spring boot starter

        Spring Boot Starter 是一种 Maven Gradle 依赖,它能够轻松地将相关库和框架集成到 Spring Boot 应用程序中。Starter 是一种对常见依赖项和设置的易于复用的封装,它们通常被开发人员用于创建可插拔的 Spring Boot 应用程序

        你可以从 Spring 官网下载不同类型的 Starters,或者通过使用 Spring Initializr 在你的 Spring Boot 项目中添加 Starters。同时,你也可以编写自己的 Starter,用于封装自己的类库和框架,并帮助其他开发人员更容易地使用它们。

        总之,Spring Boot Starter 是一种强大的工具,它可以让你快速构建强大的 Spring Boot 应用程序,减少开发和集成工作量,提高代码的可重用性和可维护性。

        例如,当我们再需要调用其他项目或服务的http接口时。可能会使用到FeginCLient、WebCleint、HttpClient、RestTemplate等。这些接口或工具都可以实现不同服务http接口之间的调用,现在我们就可以使用httpClient来封装一个可以实现远程接口调用的Starter。

新建idea项目

        新建spring boot starter项目时,和创建普通的idea项目一样File > New > Project。项目类型选择quikstart

ideal 社区版创建springboot项目 idea社区版新建springboot_java

        新建完成后目录结构如下:

ideal 社区版创建springboot项目 idea社区版新建springboot_spring_02

        文件及目录说明:

  •         org.example.config
  • META-INF 主要包含一个spring.factories文件,用于自动装配和加载 Spring Boot 应用程序的各种组件和功能。
  • application.properties
  •         pom 文件,maven构建文件以及相关依赖。

pom文件配置

        这里需要引入spring-boot-starterhttpclient

<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>

  <groupId>org.example</groupId>
  <artifactId>springboot-test-starter</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot-test-starter</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.5.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.13</version>
    </dependency>
  </dependencies>
</project>

HttpClient

HttpClient类,该类利用CloseableHttpClient 接口实现。在HttpClient新建一个单例对象closeableHttpClient用来请求http接口。以及两个字符串属性prefix(服务器根路径http://xxx.com:port/)和path(请求接口的路径)。

HttpClient中使用execute方法实现http接口调用。

package org.example.config;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

public class HttpClient {

    private static CloseableHttpClient closeableHttpClient;

    private String path;

    private String prefix;


    public void createDefault(){
        closeableHttpClient = HttpClients.createDefault();
    }
    public CloseableHttpResponse execute() throws IOException {
        if(closeableHttpClient == null){
            closeableHttpClient = HttpClients.createDefault();
        }
        return closeableHttpClient.execute(new HttpGet(String.format("%s%s",prefix,path)));
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }
}

HttpRequestProperties

        该类用来加载项目中http.server开头的接口配置。

package org.example.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("http.server")
public class HttpRequestProperties {

    private String ip;

    private String port;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }
}

HttpRequestFactory

        该类为starter 的加载类,新建该类的同时还需要在spring.factories中添加如下配置:

ideal 社区版创建springboot项目 idea社区版新建springboot_apache_03

        该配置项用于声明 Spring Boot 自动配置的实现类。添加该配置后spring boot就会自动从HttpRequestFactory中加载组件。

HttpRequestFactory.java的详细实现:

package org.example.config;


import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(HttpRequestProperties.class)
public class HttpRequestFactory {
    @Bean
    public HttpClient getHttpClient(HttpRequestProperties properties){
        HttpClient client = new HttpClient();
        client.createDefault();
        client.setPrefix(String.format("http://%s:%s/",properties.getIp(), properties.getPort()));
        return client;
    }

}

打包使用

        使用maven的package命令即可把starter项目打成jar包。

ideal 社区版创建springboot项目 idea社区版新建springboot_spring boot_04

springboot-test-starter-1.0-SNAPSHOT.jar添加到其他项目中,并且在springboot项目中添加如下配置即可使用:

http:
  server:
    # your server ip
    ip: localhost 
    # your server port
    port: 8081

        使用方法:

        注入httpClient:

@Autowired
private HttpClient httpClient;

        调用http://localhost:8081/test 接口

httpClient.setPath("/test");
CloseableHttpResponse response = httpClient.execute();
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);