手撸Spring boot stater,傻瓜都能学会

我们使用Spring boot引入其他组件的时候,会直接引入一个个的starter的依赖。比如用redis的时候会引入spring-boot-starter-data-redis,用rocketmq的时候,会引入rocketmq-spring-boot-starter。我们也可以采用这种方式,将我们自己开发的组件做成starter,让其他程序直接引用。

下面做一个demo来介绍下具体的实现方法。

一.新建项目

我们建一个名为 custom-spring-boot-starter 的springboot项目,引入如下依赖:

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

二.新增配置类

一般情况下,组件会有一些配置信息,供使用者配置。demo的配置信息如下:

package com.wpw.group.customspringbootstarter;

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

@ConfigurationProperties(prefix = "boot.custom", ignoreUnknownFields = true)
public class CustomProperties {

    private Integer port = 80;

    private String name;

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getName() {
        return name;
    }

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

三.编写组件

我们做的stater是要提供功能服务的,demo功能服务类如下,由于做demo用,功能服务类很简单,只是提供了一个doService方法返回properties的信息。

public class CustomService {

    private CustomProperties customProperties;

    public CustomService(CustomProperties customProperties) {
        this.customProperties = customProperties;
    }

    public String doService() {
        return customProperties.getName() + ":" + customProperties.getPort();
    }

}

四.新增服务配置类

配置类中,我们将功能服务类CustomService配置为Spring Bean

/**
 * 自动配置类
 */
@Configuration
@EnableConfigurationProperties(CustomProperties.class)//使使用 @ConfigurationProperties 注解的类生效。
public class CustomAutoConfiguration {

    @Bean
    public CustomService customService(CustomProperties properties) {
        return new CustomService(properties);
    }

}

五、新增配置发现文件

在resources文件夹中新建 META-INF 文件夹,在 META-INF 文件夹中新建配置发现文件 spring.factories,并且将自动配置类写到文件里。

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wpw.group.customspringbootstarter.CustomAutoConfiguration

这么做的原因是:

SpringBoot 定义了一套接口规范,这套规范规定:SpringBoot 在启动时会扫描外部引用 jar 包中的META-INF/spring.factories文件,将文件中配置的类型信息加载到 Spring 容器(此处涉及到 JVM 类加载机制与 Spring 的容器知识),并执行类中定义的各种操作。对于外部 jar 来说,只需要按照 SpringBoot 定义的标准,就能将自己的功能装置进 SpringBoot。

 经过以上的步骤,我们的starter就做好了。

六.自定义配置实现IDE自动提示

经过以上的步骤,我们的starter已经可以对外发布并正常使用了。

官方提供的spring boot starter的配置项,我们用IDE配置的时候一般都有自动提示的。而我们自己自定义的配置却没有,对开发非常不友好容易打错配置。那这个是怎样实现呢?

IDE是通过读取配置信息的元数据而实现自动提示的,而元数据在目录META-INF中的spring-configuration-metadata.json 或者 additional-spring-configuration-metadata.json。如果自己手动创建这些元数据的话工作量比较大。

我们可以在项目中引入如下依赖:

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

重新编译后,你就会发现target目录下的META-INFO文件下多了additional-spring-configuration-metadata.json文件。

七.测试

我们在另一个项目中引入我们的starter

<dependency>
	<groupId>com.wpw.group</groupId>
	<artifactId>custom-spring-boot-starter</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

配置文件中配置需要配置的参数:

boot:
  custom:
    port: 9090
    name: hello world

测试代码:

/**
 * 自定义stater的调用
 */
@RequestMapping("/starter")
@RestController
public class StarterController {

    @Resource
    private CustomService customService;

    @GetMapping("/get")
    public String get() {
        return customService.doService();
    }
}

访问该链接,页面返回:hello world:9090

大功告成

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值