自定义SpringBoot的Starter启动器

本文介绍了如何创建自定义的SpringBoot Starter启动器。按照创建模式,分为两个模块:启动器模块和自动配置模块。详细步骤包括:1) 设置启动器模块的pom配置;2) 自动配置模块的pom配置;3) 创建Properties类;4) 定义测试服务类;5) 编写自动配置类;6) 在spring.factories中配置自动配置类。完成这些步骤后,启动器可通过maven安装到本地库,并在项目中引用使用。
摘要由CSDN通过智能技术生成

创建模式:

创建两个模块:一个是启动器模块,一个是自动配置模块,启动器依赖自动配置模块;别人只需要引入启动器(Starter)就可以了。

启动器命名规则:mybatis-spring-boot-starter;自定义启动器名-spring-boot-starter

步骤:

1). 启动器模块:对应的pom配置文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?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>com.newpart.starter</groupId>

    <artifactId>newpart-spring-boot-starter</artifactId>

    <version>1.0-SNAPSHOT</version>

 

    <!--自动配置-->

    <dependencies>

        <dependency>

            <groupId>com.newpart.starter</groupId>

            <artifactId>newpart-spring-boot-starter-autoconfigurer</artifactId>

            <version>1.0-SNAPSHOT</version>

        </dependency>

    </dependencies>

 

</project>

2). 自动配置模块的pom文件

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

<?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>com.newpart.starter</groupId>

    <artifactId>newpart-spring-boot-starter-autoconfigurer</artifactId>

    <version>1.0-SNAPSHOT</version>

 

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.4.RELEASE</version>

        <relativePath/> <!-- lookup parent from repository -->

    </parent>

 

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

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

    </properties>

 

    <dependencies>

        <!--引入spring‐boot‐starter;所有starter的基本配置-->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

        </dependency>

    </dependencies>

 

 

</project>

3).创建自动配置类对应的Properties类,这个类中定义所有可以在配置文件中配置的属性

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

package com.newpart.starter;

 

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

 

/**

 */

@ConfigurationProperties(prefix = "newpart.new")

public class NewPartProperties {

 

    private String firstName;

    private String lastName;

    private Integer age;

 

    public String getFirstName() {

        return firstName;

    }

 

    public void setFirstName(String firstName) {

        this.firstName = firstName;

    }

 

    public String getLastName() {

        return lastName;

    }

 

    public void setLastName(String lastName) {

        this.lastName = lastName;

    }

 

    public Integer getAge() {

        return age;

    }

 

    public void setAge(Integer age) {

        this.age = age;

    }

}

4).定义一个自定义测试的服务类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public class NewPartService {

 

    NewPartProperties newPartProperties;

 

    public NewPartProperties getNewPartProperties() {

        return newPartProperties;

    }

 

    public void setNewPartProperties(NewPartProperties newPartProperties) {

        this.newPartProperties = newPartProperties;

    }

 

    public String sayName(String name){

        return newPartProperties.getFirstName()+name+newPartProperties.getLastName()+" age: " + newPartProperties.getAge();

    }

}

5).定义自定义自动配置类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@Configuration

@EnableConfigurationProperties(NewPartProperties.class) //指明指定配置类的属性类并注册到IOC容器中

public class NewServiceAutoConfiguratioin {

 

    @Autowired

    NewPartProperties newPartProperties;

 

    @Bean

    public NewPartService newPartService(){

        NewPartService partService = new NewPartService();

        partService.setNewPartProperties(newPartProperties);

        return partService;

    }

 

}

6). 在自动配置模块中创建spring.factories并把自动配置类配置到配置文件中

Starter启动器创建完成可以通过maven安装到本地库中在对应项目中的pom文件中引用就可以使用在自定义的定义的组件

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值