如何写一个中间件的springboot的starter?

如何写一个中间件的springboot的starter?

理解一下标题:一个springboot工程,只需要在配置文件配置相关中间件的信息,pom引入这个依赖,即可。

步骤:

  1. 新创建一个maven工程
  2. 引入springboot、中间件及相关pom依赖
  3. 编写配置类、自动装配类、服务类
  4. 编写一个spring.factories文件,把自动装配类路径写上

比如写一个elasticSearch的spring boot的starter

1.新创建一个maven工程

这里不过多赘述

2.引入springboot、中间件及相关pom依赖

我把我这里用的相关依赖贴一下,中间件的依赖不方便贴出来

<?xml version="1.0" encoding="UTF-8"?>
<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>com.lvdouwanzi</groupId>
    <artifactId>test-es-adapter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.26</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
    </dependencies>


    <build>
        <pluginManagement>
            <plugins>
                <!-- jci要求禁用官方maven-jar-plugin插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>none</phase>
                            <inherited>true</inherited>
                        </execution>
                    </executions>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                    <inherited>true</inherited>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <outputDirectory>target\javadoc</outputDirectory>
                    <reportOutputDirectory>target\javadoc</reportOutputDirectory>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <id>releases</id>
            <url>http://nexus.cbpmgt.com/nexus/content/repositories/releases</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://nexus.cbpmgt.com/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>

</project>

3.编写配置类、自动装配类、服务类

3.1:配置类(EsClientProperties.java)

这里的bean此时不会注册到spring中,在自动装配类中会手动启用该配置类

package com.lvdouwanzi.config;


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

/**
 * @author: vfudongyang
 * @createTime: 2022年06月30日 15:11:00
 * @Description:
 */
@ConfigurationProperties(prefix = "jes")
public class EsClientProperties {
    /**
     * server 地址
     */
    private String url;

    /**
     * 应用连接名
     */
    private String appName;

    /**
     * 密钥
     */
    private String secret;

    public String getUrl() {
        return url;
    }

    public String getAppName() {
        return appName;
    }

    public String getSecret() {
        return secret;
    }
}

3.2服务类(EsSearchService.java)
package com.lvdouwanzi;

import com.lvdouwanzi.config.EsClientProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PreDestroy;

/**
 * @author: vfudongyang
 * @createTime: 2022年06月30日 15:19:00
 * @Description:
 */
public class EsSearchService {

    private final static Logger log = LoggerFactory.getLogger(EsSearchService.class);

    private final EsClientProperties esClientProperties;


    public EsSearchService(EsClientProperties esClientProperties) {
        this.esClientProperties = esClientProperties;
        init();
    }


    private void init() {
        System.out.println("es client init");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("es client destory");
    }
}

3.3:自动装配类(EsAutoConfiguration.java)

注意:

  1. 这里也可以用ComponentScan去扫描该组件的类路径进行加载bean,也可以通过@bean注解去注入
  2. ConditionalOnProperty这里是规范,可以看n多中间件的starter都会有这个默认配置,这里可以理解是一个开关
  3. 构造方法会自动注入此时spring bean容器的类
package com.lvdouwanzi.start;

import com.lvdouwanzi.EsSearchService;
import com.lvdouwanzi.config.EsClientProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: vfudongyang
 * @createTime: 2022年06月30日 15:16:00
 * @Description:
 */
@Configuration
@EnableConfigurationProperties({EsClientProperties.class})
@ConditionalOnClass({EsSearchService.class})
@ConditionalOnProperty(prefix = "es",
        value = "enabled",
        matchIfMissing = true)
public class EsAutoConfiguration {

    private EsClientProperties esClientProperties;

    EsAutoConfiguration(EsClientProperties esClientProperties) {
        this.esClientProperties = esClientProperties;
    }

    @Bean
    @ConditionalOnMissingBean({EsSearchService.class})
    public EsSearchService esSearchService() {
        return new EsSearchService(esClientProperties);
    }
}

4.编写一个spring.factories文件,把自动装配类路径写上

resources 下新创建 META-INF 文件夹,文件夹新创建 spring.factories 文件

# Auto configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lvdouwanzi.start.EsAutoConfiguration
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值