以微信分享为例,快速开发一个自定义的spring boot starter

首先 pom文件:



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
         
    
    
    com.wxshare.sdk
    wxshare-spring-boot-starter
    1.0-SNAPSHOT

    
        
            org.springframework.boot
            spring-boot-autoconfigure
        

        
             org.springframework.boot 
             spring-boot-configuration-processor 
             true 
        

        
            junit
            junit
            4.12
        

        
            com.fasterxml.jackson.core
            jackson-annotations
            2.9.6
        

        
            org.apache.commons
            commons-lang3
            3.5
        

        
            org.apache.httpcomponents
            httpclient
            4.5.3
        

        
            com.thoughtworks.xstream
            xstream
            1.4.10
        

        
            com.squareup.okhttp3
            okhttp
            3.11.0
        

        
            org.apache.tomcat.embed
            tomcat-embed-core
            9.0.12
        

        
            org.slf4j
            slf4j-api
            1.7.21
        

        
            org.projectlombok
            lombok
            1.16.14
        

        
            com.squareup.retrofit2
            retrofit
            2.3.0
        

        
            com.squareup.retrofit2
            converter-simplexml
            2.3.0
        

        
        
            com.squareup.retrofit2
            converter-gson
            2.3.0
        

        
        
            org.scala-lang
            scala-library
            2.13.1
        

        
        
            com.alibaba
            fastjson
            1.2.61
        

        
            org.bouncycastle
            bcprov-jdk15on
            1.59
        
    

重点是以下两个库是自定义 starter的关键:


    org.springframework.boot
    spring-boot-autoconfigure



     org.springframework.boot 
     spring-boot-configuration-processor 
     true 

 

定义配置类:

 

package com.wxshare.sdk.config;

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

@ConfigurationProperties("wx.share")
public class WXShareProperties {

    private String baseUrl;

    private String grantType;

    private String appId;

    private String secret;

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public String getGrantType() {
        return grantType;
    }

    public void setGrantType(String grantType) {
        this.grantType = grantType;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }
}

定义需要被自动注入的配置类

 

package com.wxshare.sdk.autoconfigure;

import com.wxshare.sdk.config.WXShareProperties;
import com.wxshare.sdk.remoting.WXShareRemoting;
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(WXShareRemoting.class)
@EnableConfigurationProperties(WXShareProperties.class)
public class WXShareAutoConfigure {

    @Bean
    @ConditionalOnMissingBean
    WXShareRemoting wxShareRemoting(){
        return new WXShareRemoting();
    }
}


最后一步,在resources/META-INF/下创建spring.factories文件,内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.wxshare.sdk.autoconfigure.WXShareAutoConfigure

OK,完事,运行 mvn:install打包安装,一个Spring Boot Starter便开发完成了,引用 WXShareRemoting  的方式跟你引用其他starter的方式一样

有问题可加微信 

源码已提交到码云:https://gitee.com/tianji_luhaichuan/pay/tree/master/wxshare-springboot-starter

 

你可能感兴趣的:(spring,boot)