实现一个SpringBootStarter

功能实现

SpringBootStarter可以将需要的功能整合起来,方便使用。这个例子是一个非常简单的Starter实现,关键在于走通流程。首先单独创建一个maven项目,命名为greeter-spring-boot-starter。在项目中创建一个配置定义类GreeterProperties,主要定义一个配置前缀和对应的属性,一个starter核心类GreeterAutoConfiguration,通过这个实现基于starter方式的加载,如下:

@Configuration
@ConditionalOnClass(Greeter.class)
@ConditionalOnProperty(prefix = "igoso.greeter",value = "enable",matchIfMissing = true)
@EnableConfigurationProperties(GreeterProperties.class)
public class GreeterAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public Greeter greeter() {
        return new Greeter();
    }
}

最后一个业务类Greeter,一个根据当前时间打招呼的逻辑,如下:

package com.igoso.me.autoconfigure;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;

@Service
public class Greeter {

    @Autowired
    private GreeterProperties greeterProperties;

    private String sayMorning() {
        return greeterProperties.getMorningMsg();
    }

    private String sayAfternoon() {
        return greeterProperties.getAfternoonMsg();
    }

    private String sayEvening() {
        return greeterProperties.getEveningMsg();
    }


    /**
     * 外部调用方法
     * @return
     */
    public String sayHello() {
        String msg;
        int h = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        if (h < 8) {
            msg = sayMorning();
        } else if (h >= 12 && h <= 16) {
            msg = sayAfternoon();
        } else {
            msg = sayEvening();
        }

        return "[" + greeterProperties.getUserName() + "] " + msg;
    }
}

配置定义的类GreeterProperties

package com.igoso.me.autoconfigure;

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

@ConfigurationProperties(prefix = "igoso.greeter")
public class GreeterProperties {
    private String userName;
    private String morningMsg;
    private String afternoonMsg;
    private String eveningMsg;
    private String nightMsg;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getMorningMsg() {
        return morningMsg;
    }

    public void setMorningMsg(String morningMsg) {
        this.morningMsg = morningMsg;
    }



    public String getEveningMsg() {
        return eveningMsg;
    }

    public void setEveningMsg(String eveningMsg) {
        this.eveningMsg = eveningMsg;
    }

    public String getNightMsg() {
        return nightMsg;
    }

    public void setNightMsg(String nightMsg) {
        this.nightMsg = nightMsg;
    }

    public String getAfternoonMsg() {
        return afternoonMsg;
    }

    public void setAfternoonMsg(String afternoonMsg) {
        this.afternoonMsg = afternoonMsg;
    }
}

另外在resource里需要定义META-INF信息。建立META-INF文件夹,将以下信息写入spring.factories文件,springBoot就是通过这个文件加载Starter的。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.igoso.me.autoconfigure.GreeterAutoConfiguration

以上项目pom.xml文件依赖配置,只需要spring-boot-autoconfigure就可以。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

完成后mvn install,部署到本地仓库,就可以使用了。如果是生产或者企业开发环境,使用mvn deploy部署到远程仓库。

具体使用方式

在上面的步骤完成添加到本地仓库后,就可以在另外的项目中添加starter依赖,注意默认的版本号。pom.xml配置如下:

        <dependency>
            <groupId>com.igoso.me</groupId>
            <artifactId>greeter-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

配置信息写入application.properties里,就可以调用具体的Greeter逻辑方法。

igoso.greeter.userName=netscape
igoso.greeter.morningMsg=中午好
igoso.greeter.afternoonMsg=下午好
igoso.greeter.eveningMsg=晚上好

以上,简单做个记录。