自定义springboot starter的几种方式

四种方式

  • main可以扫描到的包下,直接配置 xxxAutoConfiguration
  • 推荐main扫描不到的情况,配置到spring.factories中,然main方法扫描读取在src/main/resources/META-INF下创建spring.factories文件
  • 推荐main扫描不到的情况,自定义Enablexxx注解
  • main扫描不到的情况,不需要自定义EnableHttpClient注解,直接使用@Import在main类中

一下例子均以httpClient为例


方法一:main可以扫描到

main在com.mooc.house下

自定义starter在com.mooc.house.autoconfig下

添加依赖


    org.apache.httpcomponents
    httpclient

HttpClientAutoConfiguration.java

package com.mooc.house.autoconfig;
 
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.NoConnectionReuseStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
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({HttpClient.class})
@EnableConfigurationProperties(HttpClientProperties.class)
public class HttpClientAutoConfiguration {
    
    private final HttpClientProperties properties;
    
    public HttpClientAutoConfiguration(HttpClientProperties  properties){
        this.properties = properties;
    }
    
    /**
     * HttpClient bean 的定义
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(HttpClient.class)
    public HttpClient httpClient(){
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(properties.getConnectTimeOut())
                .setSocketTimeout(properties.getSocketTimtOut()).build(); // 构建requestConfig
        HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig)
                .setUserAgent(properties.getAgent()).setMaxConnPerRoute(properties.getMaxConnPerRoute())
                .setMaxConnTotal(properties.getMaxConnTotal())
                .setConnectionReuseStrategy(new NoConnectionReuseStrategy()).build();
        
        return client;
    }
    
}

HttpClientProperties.java

package com.mooc1.house.autoconfig;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
 
@ConfigurationProperties(prefix="spring.httpclient")
public class HttpClientProperties {
 
    private String agent = "agent";
    
    private Integer maxConnPerRoute = 10;
    
    private Integer maxConnTotal = 50;
    
    private Integer connectTimeOut = 1000;
    
    private Integer socketTimtOut = 1000;
 
    public String getAgent() {
        return agent;
    }
 
    public void setAgent(String agent) {
        this.agent = agent;
    }
 
    public Integer getMaxConnPerRoute() {
        return maxConnPerRoute;
    }
 
    public void setMaxConnPerRoute(Integer maxConnPerRoute) {
        this.maxConnPerRoute = maxConnPerRoute;
    }
 
    public Integer getMaxConnTotal() {
        return maxConnTotal;
    }
 
    public void setMaxConnTotal(Integer maxConnTotal) {
        this.maxConnTotal = maxConnTotal;
    }
 
    public Integer getConnectTimeOut() {
        return connectTimeOut;
    }
 
    public void setConnectTimeOut(Integer connectTimeOut) {
        this.connectTimeOut = connectTimeOut;
    }
 
    public Integer getSocketTimtOut() {
        return socketTimtOut;
    }
 
    public void setSocketTimtOut(Integer socketTimtOut) {
        this.socketTimtOut = socketTimtOut;
    }
    
}

方式二、main扫描不到的情况,配置到spring.factories中,然main方法扫描读取

在src/main/resources/META-INF下创建spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mooc1.house.autoconfig.HttpClientAutoConfiguration

方式三、main扫描不到的情况,自定义EnableHttpClient注解

package com.mooc1.house.autoconfig;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
import org.springframework.context.annotation.Import;
 
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(HttpClientAutoConfiguration.class)
public @interface EnableHttpClient {
    
}

main方法类中使用注解

package com.mooc.house;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
import com.mooc1.house.autoconfig.EnableHttpClient;
 
@SpringBootApplication
@EnableHttpClient
public class HouseApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(HouseApplication.class, args);
    }
 
}

方式四、main扫描不到的情况,不需要自定义EnableHttpClient注解,直接使用@Import在main类中

package com.mooc.house;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
 
import com.mooc1.house.autoconfig.HttpClientAutoConfiguration;
 
@SpringBootApplication
//@EnableHttpClient
@Import(HttpClientAutoConfiguration.class)
public class HouseApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(HouseApplication.class, args);
    }
 
}


参考资料:

https://blog.csdn.net/zhangminemail/article/details/89066408

自定义配置文件提示功能spring-configuration-metadata
https://zhuanlan.zhihu.com/p/77175198

你可能感兴趣的:(自定义springboot starter的几种方式)