Spring5 全家桶 | 46 - Spring Boot 2.x 自定义 Starter

1,067 阅读4分钟

“Offer 驾到,掘友接招!我正在参与2022春招打卡活动,点击查看活动详情。”

一、Spring Boot中的Starters

Spring Boot官网中关于Starters的介绍

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.

Starters既启动器是一组方便的依赖项描述符,在项目中应用这些Starters。您可以获得所需的所有Spring和相关技术的一站式服务,而无需查找示例代码和复制-粘贴大量依赖描述符。例如,如果您想开始使用Spring和JPA进行数据库访问,请在您的项目中包含Spring -boot-starter-data-jpa依赖项。

二、自定义Staters

Spring Boot官网中关于自定义Starters的介绍 7.9.5. Creating Your Own Starter

自定义Starter首先要确定场景的依赖,然后自定义Starter会用到以下注解或者配置。需要说明的是启动器是一个空的JAR文件,仅仅提供辅助性的依赖管理,这些依赖可能用于自动装配或者其他类库。

自定义Starter需要遵循一些命名约定

  • Spring Boot 官方Starter的前缀名为”spring-boot-starter-“,命名为“spring-boot-starter-模块名”,如spring-boot-starter-web、spring-boot-starter-jdbc
  • 第三方的Starter后缀名为“-spring-boot-starter”,命名为“模块名-spring-boot-starter”,如”mybatis-spring-boot-starter、druid-spring-boot-starter

首先创建一个Empty Project,将会在这个Project中创建Starter启动器工程lilith-spring-boot-starter和autoconfigure自动配置工程lilith-spring-boot-starter-autconfigure,lilith-spring-boot-starter启动器是一个空的JAR文件,仅仅提供辅助性的依赖管理。

接着在这个空工程里面添加Module,创建一个Maven工程作为启动器Starter lilith-spring-boot-starter。

然后再创建Spring Boot工程作为自动配置类lilith-spring-boot-starter-autconfigure,使用Spring Initializr工具创建 image.png

image.png

至此在这个Empty Project中共创建两个Module,分别是lilith-spring-boot-starter和lilith-spring-boot-starter-autconfigure

image.png 第一个是启动器,第二个是负责自动配置的模块

首先在启动器Starter中引入自动配置模块的依赖,在lilith-spring-boot-starter模块的pom.xml文件中增加

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <optional>true</optional>
</dependency>

而lilith-spring-boot-starter-autoconfigure模块主要用来做自动配置,所以该模块下的主程序类以及配置文件和测试包可以删除,在pom.xml文件中只需要引入spring-boot-starter依赖即可,spring-boot-starter是所有starter的基本配置

自定义starter的需求是通过引入starter可以使用该starter中的Lilith类,使用该类可以通过配置打印出不同语言的”Hallo Lilith“

Lilith类代码,包含了一个LilithProperties类,用来定义配置项,hallo方法用来输出文本

public class Lilith {

    private LilithProperties lilithProperties;

    public LilithProperties getLilithProperties() {
        return lilithProperties;
    }

    public void setLilithProperties(LilithProperties lilithProperties) {
        this.lilithProperties = lilithProperties;
    }

    public String hallo(String name){
        return lilithProperties.getLanguages() + " " + name;
    }

}

接着需要定义一个配置项的类LilithProperties,使用@ConfigurationProperties标记配置的prefix

@ConfigurationProperties(prefix = "lilith")
@Component
public class LilithProperties {

    // 语种
    private String languages;

    public String getLanguages() {
        return languages;
    }

    public void setLanguages(String languages) {
        this.languages = languages;
    }
}

增加一个自动配置类LilithAutoConfiguration

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(LilithProperties.class)
public class LilithAutoConfiguration {

    @Autowired
    private LilithProperties lilithProperties;

    @Bean
    public Lilith lilith(){
        Lilith lilith = new Lilith();
        lilith.setLilithProperties(lilithProperties);
        return lilith;
    }
}

resources目录下创建一个文件夹META-INF/spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lilith.starter.autoconfigure.LilithAutoConfiguration

左侧目录结构如下

image.png

自动配置模块完成,自动配置类会往容器中添加Lilith类,Lilith类中用到的属性与LilithProperties类中的属性绑定

image.png

然后install到本地的maven仓库中

三、测试自定义的Starter

使用Spring Initializr工具创建一个新的工程spring-boot-lilith,选择Spring Web依赖即可,再引入自定义的starter,在pom.xml文件中添加以下依赖

<dependency>
    <groupId>com.lilith.starter</groupId>
    <artifactId>lilith-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

新建controller包,增加HalloController

@RestController
public class HalloController {

    @Autowired
    private Lilith lilith;

    @GetMapping("/hallo")
    public String hallo(){
        return lilith.hallo("Lilith");
    }
}

接着在application.properties中进行配置

lilith.languages=Buon Giorno

启动应用,在浏览器输入 http://localhost:8080/hallo image.png

页面上成功显示了意大利语的你好 Lilith

类似的例子还有阿里巴巴的Druid数据源以及MyBatis Plus,在Druid Starter出现之前都是通过书写配置类的方式既通过@Configuration和@Bean注解将Druid的DataSource导入到容器中,通过引入Stater的方式就可以省略配置类。