如何编写自己的Spring Boot的Starter

Spring Boot的Starter是一个很优秀的东西。Starter为我们实现各种功能提供了一站式的接入方式。比如我们要在项目中使用JPA,那么直接引入spring-boot-starter-data-jpa就可以了。spring-boot-starter-data-jpa包括了引入jpa需要依赖的其他第三方包,以及要开启JPA的相关默认配置。Starter可以让我们对某个特性的需求做到开箱即用。

随着Spring Boot的流行,Starter越来越多了。官方的Starter命名都是spring-boot-starter-*这样的格式。*一般根据特性来指定。也有一些第三方Starter,但是第三方Starter一般命名都是thirdpartproject-spring-boot-starter,第三方的Starter不以spring-boot-starter开头。Starter是很好创建的,官方提供了指导:Creating Your Own Starter.

官方将自己的Starter分了下类: Starter 这里有三个Table. 占篇幅比较大,大家点前面链接进去看就好。
三大类分别是引入功能特性的Starter、为生产监控准备的Starter、以及可以自定义某种技术方案的Starter。
功能特性的Starter很好理解,比如前面提到的spring-boot-starter-data-jpa。为生产监控准备的Starter只有一个,就是spring-boot-starter-actuator、最后还有一类,这类主要是为了切换项目中的某些技术方案,比如默认容器是Tomcat,但是咱就喜欢jetty,那就引入spring-boot-starter-jetty。从spring-boot-starter-web中排除spring-boot-starter-tomcat

Spring Boot官方提供了很多的Starter,每个Starter都有其各自的功能。我们用起来也爽歪歪。同时,我们也可以自定义一些Starter,提供出来给其他人用。

本文创建一个及其简单的Starter,这个Starter用来在启动的时候初始化Person对象。

首先,去 Spring Initializr 创建一个Spring Boot的项目(Starter仍旧是一个Spring Boot Application),如图1.

图1 创建基础项目

创建的过程中不需要添加依赖。同时,去掉生成的pom.xml中的下面代码:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

这部分代码是用来打包的,他需要main方法,但是我们的Starter中是不需要main方法的。所以这个插件要去掉。

然后修改生成的启动类。修改后的代码如下:

    package cn.com.hanbinit.printspringbootstarter;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class PrintSpringBootStarterApplication {
        private static final Logger logger = LoggerFactory.getLogger(PrintSpringBootStarterApplication.class);
    
        @Value("${hanbin.name:}")
        private String name;
    
        @Bean
        public Person initPerson(){
            logger.info("我是自定义starter里面打印的,我只会在服务启动的时候初始化一下!");
            return new Person(name);
        }
    
    }

在同目录下创建了Person类,内容如下:

    package cn.com.hanbinit.printspringbootstarter;
    
    public class Person {
        private String name;
    
        public Person(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

最后,还有一个很重要的事情,在resources下建立META-INF目录,在目录下创建spring.factories文件,在文件中添加如下配置:

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    cn.com.hanbinit.printspringbootstarter.PrintSpringBootStarterApplication

到这里,这个Starter就创建完成了。这个时候我们就可以mvn clean install 或者 deploy给其他人用了。

下面创建一个demo application,引用了spring-boot-starter-web依赖,我们要在它中使用上面的Starter。

首先,在demo工程的pom.xml中添加依赖:

    <dependency>
        <groupId>cn.com.hanbinit</groupId>
        <artifactId>print-spring-boot-starter</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

同时,修改启动类为下面的代码:

    package cn.com.hanbinit.demo;
    
    import cn.com.hanbinit.printspringbootstarter.Person;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @SpringBootApplication
    public class DemoApplication {
    
        private final Person person;
    
        public DemoApplication(Person person) {
            this.person = person;
        }
    
        @GetMapping("/get_name")
        public String print(){
            return person.getName();
        }
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }

在application.properties中配置 hanbin.name=wo shi hanbin

访问http://localhost:8080/get_name,可得图2所示结果:

图2

关于Starter创建更多的可以多看看官方文档,也可以看看spring-boot-autoconfigure包的代码。Starter的技术基础还是Spring Boot的Auto-Configuration机制。