SpringBoot-自定义Starter

步骤

  • 创建Stater 项目
  • 定义Starter 需要的配置类
  • 编写自动配置类
  • 编写spring.factories 文件加载自动配置类
  • 编写配置提示文件spring-configuration-metadata.json (非必需)

示例

使用IDEA 新建一个Maven 项目。
pom.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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>cn.vgbhfive</groupId>
<artifactId>springbootstarterdemo</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>

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

</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

目录文件:
1.jpg

  1. 配置参数类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package cn.vgbhfive.properties;

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

    /**
    * @time: 2019/08/13
    * @author: Vgbh
    */
    @Data
    @ConfigurationProperties("spring.vgbhfive") // 配置参数前缀
    public class DemoProperties {

    private String name; // 配置参数

    }
  2. 自动配置类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package cn.vgbhfive.config;

    import cn.vgbhfive.client.VgbhfiveClient;
    import cn.vgbhfive.properties.DemoProperties;
    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;

    /**
    * @time: 2019/08/13
    * @author: Vgbh
    */
    @Configuration //开启配置
    @EnableConfigurationProperties(DemoProperties.class) // 开启使用映射实体
    public class VgbhfiveAutoConfigure {

    @Bean
    public VgbhfiveClient vgbhfiveClient(DemoProperties demoProperties) {
    //System.out.print(" ------- VgbhfiveClient Created! -------");
    return new VgbhfiveClient(demoProperties);
    }

    }
  3. 服务类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package cn.vgbhfive.client;

    import cn.vgbhfive.properties.DemoProperties;

    /**
    * @time: 2019/08/13
    * @author: Vgbh
    */
    public class VgbhfiveClient {

    private DemoProperties demoProperties; // 配置参数

    public VgbhfiveClient () {
    }

    public VgbhfiveClient(DemoProperties p) {
    this.demoProperties = p;
    }

    public String getName() {
    return demoProperties.getName();
    }

    }
  4. 开启服务类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    package cn.vgbhfive.annotation;

    import cn.vgbhfive.config.VgbhfiveAutoConfigure;
    import org.springframework.context.annotation.Import;

    import java.lang.annotation.*;

    /**
    * @time: 2019/08/14
    * @author: Vgbh
    */
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @Import({VgbhfiveAutoConfigure.class})
    public @interface EnableVgbhfiveCient {
    }
  5. 扫描自动配置类文件

    1
    2
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    cn.vgbhfive.config.VgbhfiveAutoConfigure
  6. 配置参数提醒

    1
    2
    3
    4
    5
    6
    7
    8
    {
    "properties":[
    {
    "name":"spring.vgbhfive.user",
    "defaultValue":"vgbhfive"
    }
    ]
    }

备注

Spring Boot 官方建议个人开发使用xxx-spring-boot-starter 这种格式作为包名。

存放地址

https://github.com/vgbhfive/SpringBootDemo -> springbootstarterdemo


使用自定义Starter 项目

pom.xml :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.vgbhfive</groupId>
<artifactId>springbootstarterdemotest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootstarterdemotest</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

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

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

<dependency>
<groupId>cn.vgbhfive</groupId>
<artifactId>springbootstarterdemo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

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

</project>
  1. 配置文件application.properties

    1
    spring.vgbhfive.name=vgbh
  2. 开启服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package cn.vgbhfive.springbootstarterdemotest;

    import cn.vgbhfive.annotation.EnableVgbhfiveCient;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @EnableVgbhfiveCient
    @SpringBootApplication
    public class SpringbootstarterdemotestApplication {

    public static void main(String[] args) {
    SpringApplication.run(SpringbootstarterdemotestApplication.class, args);
    }

    }
  3. 对外预留接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    package cn.vgbhfive.springbootstarterdemotest;

    import cn.vgbhfive.client.VgbhfiveClient;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    /**
    * @time: 2019/08/14
    * @author: Vgbh
    */
    @RestController
    public class DemoController {

    @Autowired
    private VgbhfiveClient vgbhfiveClient;

    @GetMapping("/demo")
    public String test() {
    String name = vgbhfiveClient.getName();
    if (null == name) {
    return "Hello World!";
    }
    return name;
    }

    }
  4. 输出结果
    2.jpg


实战项目

https://github.com/vgbhfive/vid-spring-boot-starter


参考资料


个人备注

此内容均为作者学习所做笔记!
转做其他用途必经作者同意,假冒转载必究!