专栏名称: hryou0922
目录
相关文章推荐
阑夕  ·  ? 阑夕的微博视频 -20250312200932 ·  10 小时前  
纯银V  ·  我 2019 ... ·  16 小时前  
界面新闻  ·  盘中大涨超15%!老铺黄金总市值突破1300亿港元 ·  20 小时前  
界面新闻  ·  DeepSeek R2发布?回应来了 ·  昨天  
槽边往事  ·  彩虹的教授 ·  2 天前  
51好读  ›  专栏  ›  hryou0922

Spring Boot系列七 实现自己的spring boot starter工程

hryou0922  · 掘金  ·  · 2018-01-27 13:20

正文

1. 概述

spring boot starter工程对于使用者是非常方便的,使用者通常只要在pom.xml引入starter的jar,则此工程依赖的类,就全部自动引入。因此我们常用的开源组件都会提供一个starter工程给开发者,让开发者非常方便集成本组件到spring中。本文通过自己实现一个简单的demo,详细说明了如何实现一个自己的spring boot starter工程。

2. 例子中工程说明

本文demo参考mybatis-spring-boot-starter和网上的文章创建自己的start工程。

定义我们自己的spring boot starter工程,通常需要以下4个工程:

  1. 模块的真正的实现类,如本文的工程为first
  2. *-spring-boot/*-spring的工程:此工程是*-spring-boot-autoconfigure和*-spring-boot-starter的父工程类。本例子里是first-spring-boot。此工程的命名规则如下:如果你在这个工程中引入spring依赖,则工程名称为*-spring,如果引入spring-boot依赖,则工程名称为*-spring-boot。
  3. *-spring-boot-autoconfigure工程:配置自动配置类,本例的名称first-spring-boot-autoconfigure
  4. *-spring-boot-starter工程:空工程类,在pom.xml中定义需要引入jar,本例的名称first-spring-boot-starter

测试工程:

  • test-main:演示如何使用自己的first-spring-boot-starter

例子的功能说明:

  • 定义ITestService接口和此接口的两个实现类,根据我们的配置,程序自动初始化指定的一个类。

3. first工程

此工程中不要引入扫描相关的注解,如@Autowire, @Componet, @Service, @Control等等; 不要引入spring 相关的类

本工程功能功能: 定义ITestService接口和此接口的两个实现类

定义ITestService接口

public interface ITestService {

    String queryById(String id);
}

此接口的两个实现类

  • TestServiceImpl
public class TestServiceImpl implements ITestService {
    @Override
    public String queryById(String id) {
        return id + ":" + this.getClass().getSimpleName() ;
    }

}
  • TestServiceBackImpl
public class TestServiceBackImpl implements ITestService {

    @Override
    public String queryById(String id) {
        return id + ":" + this.getClass().getSimpleName() ;
    }
}

4. first-spring-boot

此工程中也不要引入扫描相关的注解,如@Autowire, @componet, @Service, @Controller等等 此类可以添加自己的类,这些类需要使用到spring-boot里的类。我们的例子里不需要添加和spring-boot相关的类,所有此工程没有java类。

这个工程主要定义子模块的需要用到的jar

4.1. pom.xml

定义版本信息和属性值,packaging值为pom

   <artifactId>first-spring-boot</artifactId>
    <groupId>com.hry.spring.first</groupId>
    <version>1.0.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>
    <!-- 类型为pom -->
    <packaging>pom</packaging>

    <properties>
        <first.version>1.0.0-SNAPSHOT</first.version>
        <spring-boot.version>1.5.6.RELEASE</spring-boot.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

定义两个子模块

 <modules>
     <module>../first-spring-boot-autoconfigure</module>
     <module>../first-spring-boot-starter</module>
 </modules>

由于不是所有的子模块都会用到以下的类,所有使用dependencyManagement预先定义相关的jar和版本。保证在子类中需要使用时,只需要引入包,而不用使用版本号

<!-- 定义子模块中需要的jar -->
<dependencyManagement>
    <dependencies>
        <!-- spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <!-- first model -->
        <dependency>
            <groupId>com.hry.spring.first</groupId>
            <artifactId>first</artifactId>
            <version>${first.version}</version>
        </dependency>
        <!-- first-spring-boot-autoconfigure -->
        <dependency>
            <groupId>com.hry.spring.first</groupId>
            <artifactId>first-spring-boot-autoconfigure</artifactId>
            <version>${first.version}</version>
        </dependency>
        <!--  first-spring-boot-starter -->
        <dependency>
            <groupId>com.hry.spring.first</groupId>
            <artifactId>first-spring-boot-starter</artifactId>
            <version>${first.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

5. first-spring-boot-autoconfigure

配置自动扫描类

5.1 FirstProperties

属性配置类,自动从配置文件中解析属性并生成对象

@ConfigurationProperties(prefix = FirstProperties.TEST_PREFIX)
public class FirstProperties {
   public static final String TEST_PREFIX = "my.test";

   private boolean openTestServiceBack; // 如果true,则创建openTestService,否则使用TestServiceBackImpl

   private String name;

   public boolean isOpenTestServiceBack() {
       return openTestServiceBack;
   }

   public void setOpenTestServiceBack(boolean openTestServiceBack) {
       this.openTestServiceBack = openTestServiceBack;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }
}

5.2. FirstModuleAutoConfiguration

  • @Configuration:定义配置类
  • @EnableConfigurationProperties:启动上面的配置类

    createTestService方法:根据配置的属性生成ITestService 的实现类

@Configuration
@EnableConfigurationProperties(FirstProperties.class)
public class FirstModuleAutoConfiguration {

    @Autowired
    private FirstProperties firstProperties;

    @Bean
    public ITestService createTestService(){
        if(firstProperties.isOpenTestServiceBack()){
            return new TestServiceBackImpl();
        }else{
            return new TestServiceImpl();
        }
    }

}

5.3. @AutoConfigure*和Conditional*

如果我们的FirstModuleAutoConfiguration 的初始化必须依赖其他的自动化配置类(如OtherModuleAutoConfiguration.class),则在此类上增加以下注解

@AutoConfigureAfter(OtherModuleAutoConfiguration.class)

我们还可以在配置类定义以下注解

  1. @AutoConfigureAfter: 提示自动配置应在其他指定的自动配置类之后应用。
  2. @AutoConfigureBefore: 提示自动配置应在其他指定的自动配置类之前应用
  3. @AutoConfigureOrder:定义配置类执行的顺序

另外我们也可以加上以下@Conditional条件注解







请到「今天看啥」查看全文


推荐文章
阑夕  ·  ? 阑夕的微博视频 -20250312200932
10 小时前
界面新闻  ·  DeepSeek R2发布?回应来了
昨天
槽边往事  ·  彩虹的教授
2 天前
狮子座网  ·  千万别对狮子座撒谎!
7 年前
人民日报  ·  来了!新闻早班车
7 年前
OFweek半导体照明  ·  LED植物生长灯知识,你知多少?
7 年前