Springboot 开发 -- 创建Spring Boot Starter

一、简介

Spring Boot Starter是Spring Boot生态中非常重要的一部分,它允许开发者通过简单的依赖管理来快速集成各种功能和库。在开发过程中,我们经常会遇到一些通用的功能或配置,如果每次都需要手动添加这些配置和依赖,那么将会非常繁琐。因此,创建一个自定义的Spring Boot Starter可以极大地提高开发效率。本文将详细介绍如何创建自己的Spring Boot Starter,并为其编写单元测试。

二、创建Spring Boot Starter

1. 初始化项目

使用Spring Initialize(https://start.spring.io/)或Maven/Gradle手动创建一个新的Spring Boot项目,并选择需要的依赖(通常不需要选择太多,因为我们只是创建一个starter)。

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

2. 定义自动配置

在src/main/java目录下,创建一个新的包(例如com.example.mystarter.autoconfigure),并在其中添加自动配置类。这个类需要使用@Configuration和@EnableAutoConfiguration注解,并可能需要定义一些@Bean方法。

package com.example.mystarter.autoconfigure;  
  
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.context.annotation.Bean;  
  
@Configuration  
@EnableAutoConfiguration  
public class MyStarterAutoConfiguration {  
  
    @Bean  
    public MyService myService() {  
        return new MyServiceImpl();  
    }  
}

3.创建spring.factories文件(Spring Boot 2.7 以下)

在src/main/resources/META-INF目录下创建spring.factories文件,并在org.springframework.boot.autoconfigure.EnableAutoConfiguration关键字下列出您的自动配置类,比如:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.mystarter.autoconfigure.MyStarterAutoConfiguration 

该配置的作用是让Spring Boot应用在引入您自定义Starter的时候可以自动这里的配置类。

4. Spring Boot 2.7 新特性

Spring Boot 2.7开始,不再推荐使用spring.factories,而是创建新的文件:

 /META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

文件内容直接放需要自动加载配置类路径即可,比如这样:

com.example.mystarter.autoconfigure.MyStarterAutoConfiguration 

注意:这里多了一级spring目录。

三、验证测试

在制作Spring Boot Starter的时候,一定记得使用单元测试来验证和确保自动化配置类在任何条件逻辑在启动器下能够按照正确的预期运行。

1. 添加测试依赖

在pom.xml或build.gradle文件中添加测试相关的依赖,例如Spring Boot Test、JUnit等。

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

2. 创建单元测试

使用@SpringBootTest加载完整的应用程序上下文,并验证启动程序是否正确配置了 Bean 和属性。

package com.example.mystarter;  
  
import com.example.mystarter.autoconfigure.MyStarterAutoConfiguration;  
import com.example.mystarter.service.MyService;  
import org.junit.jupiter.api.Test;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.boot.test.context.SpringBootTest;  
  
import static org.junit.jupiter.api.Assertions.assertNotNull;  
  
@SpringBootTest(classes = MyStarterAutoConfiguration.class)  
public class MyStarterAutoConfigurationTest {  
  
    @Autowired  
    private MyService myService;  
  
    @Test  
    public void testMyService() {  
        assertNotNull(myService, "MyService should not be null");  
        // 这里可以添加更多的测试逻辑  
    }  
}

参考:
https://segmentfault.com/a/1190000020121457

  • 14
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值