SpringBoot自定义starter简单版(一)

1. 情况说明

之前试探性的面试了一家公司,其中有一个问题是:有没有自己写过一个SpringBoot自定义starter

由于之前阅读过Spring的源码,这还是问题嘛?虽然我没有写过,但我把原理说了一遍,那抽空就来写一个自定义的starter吧,还要写个加强版的。

这一篇先写个简单的吧。

2. 原理

starter的原理很简单:就是SpringBoot在启动的过程中,会拿到类路径下所有jar包中的META-INF/spring.factories文件,拿到里面的org.springframework.boot.autoconfigure.EnableAutoConfiguration配置项,来判断配置的类是否满足自动装配的条件。

但写一个自定义的starter,还需要有一些额外只是的了解

  • @ConfigurationProperties注解作用

  • @EnableConfigurationProperties注解作用

下面这两篇文章可以了解其自动配置的原理和相关注解的使用,在写之前,可以先阅读一下。

SpringBoot的starter启动机制分析

https://blog.csdn.net/zxd1435513775/article/details/99064671

Spring中读取配置属性的方式和SpringBoot自动配置重要特性

https://blog.csdn.net/zxd1435513775/article/details/103661672

3. 步骤

  • 引入对应的依赖
  • 编写自动配置类:使用@EnableConfigurationProperties注解、@Configuration注解
  • 自动配置类与相关属性类绑定:使用@ConfigruationProperties注解
  • 编写具体实现类
  • resources/META-INF/spring.factories中配置自定义的自动装配类

4. 代码实现

目录结构

在这里插入图片描述

4.1 pom文件

最后要把自定义的starter工程打成jar,让其他工程引用

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

4.2 自动配置类

对于这个自动配置类,要配置在META-INF/spring.factories文件里。一旦让这个自动配置类生效,就可以做很多事情啦。下一篇加强版就在这地方做文章的。

@Slf4j
@Configuration
// 绑定属性配置类
@EnableConfigurationProperties(CustomConfigurationProperties.class)
@ConditionalOnClass(CustomService.class) // 当类路径下有指定类 改配置才有效
public class CustomConfigurationClass {

    @Bean
    public CustomService customService(){
        log.info("customService....");
        return new CustomServiceImpl();
    }
}

4.3 属性配置类

@ConfigurationProperties("custom.node")
public class CustomConfigurationProperties {

    // 给默认值
    private String customHost = "127.0.0.1";

    private String customPort = "8080";

    public String getCustomHost() {
        return customHost;
    }

    public void setCustomHost(String customHost) {
        this.customHost = customHost;
    }

    public String getCustomPort() {
        return customPort;
    }

    public void setCustomPort(String customPort) {
        this.customPort = customPort;
    }
}

4.4 具体业务类

这个具体业务类,这地方可写的东西太多了,可做的事情也太多了,这里只写了测试代码。

@Slf4j
@Service
public class CustomServiceImpl implements CustomService {

    @Autowired
    CustomConfigurationProperties cp;

    @Override
    public void customMethod() {
        log.info("customMethod invoke ...");
        log.info(cp.getCustomHost()+" : " + cp.getCustomPort());
    }

}

4.5 spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.scorpios.customstarter.config.CustomConfigurationClass

5. 测试

项目启动日志

在这里插入图片描述

application.properties中没有配置custom.node.customHostcustom.node.customPort相关信息,测试结果如下:

在这里插入图片描述

application.properties中添加如下配置,测试结果如下:

custom.node.customHost=192.168.3.1
custom.node.customPort=8888

在这里插入图片描述

自定义starter就这么写完了,就是几个知识点的运用。具体代码地址如下:

代码地址:https://github.com/Hofanking/springboot-custom-starter-example

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

止步前行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值