一、什么是Starter
在Spring Boot框架中,Starter(起始器)是一种特殊的依赖管理机制,它为开发者提供了一种便捷的方式来快速集成和配置特定的功能模块。Spring Boot Starter本质上是一个Maven或Gradle项目中的依赖描述符(POM或dependencies),它预定义并聚合了一系列相关的库及其依赖关系。
每个Spring Boot Starter都是为了实现某一特定功能而设计的,例如:
spring-boot-starter-web:用于快速搭建基于Spring MVC的Web应用,包含了Tomcat容器、Spring Web以及相关依赖。
spring-boot-starter-data-jpa:提供了JPA支持,集成了Hibernate等ORM工具,简化了数据库访问层的配置。
spring-boot-starter-actuator:包含了一系列监控和管理Spring Boot应用的端点。
通过引入对应的Spring Boot Starter到项目的构建文件中,开发者无需手动添加多个底层依赖项,只需要一个starter即可启用所需功能,并且自动配置会根据类路径中的组件进行合理默认设置,极大地提高了开发效率和可维护性。同时,这也允许开发者基于自己的需求定制自定义的Spring Boot Starter来封装特定的公用代码和配置
二、Starter 命名规则
Spring 官方定义的 Starter 通常命名遵循的格式为 spring-boot-starter-{name},例如 spring-boot-starter-data-mongodb。Spring 官方建议,非官方 Starter 命名应遵循 {name}-spring-boot-starter 的格式,例如,myjson-spring-boot-starter。
三、自定义一个 Starter
项目目录如下
1、在pom.xml文件中加入实现 starter 所需要的依赖
<!--实现 starter 所需要的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
2、创建配置类,这个配置类能够将核心功能类注入到 Ioc 容器,使得在其他引用此 starter 的项目中能够使用自动配置的功能。
@Configuration 注解的作用是将 MyConfiguration 类作为配置类注入容器
@ComponentScan主要就是扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中
package com.bobo.config;
import com.bobo.service.StartTestService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author GBo
* @date 2024/3/5 14:37
*/
@Configuration
@ComponentScan(basePackages = {"com.bobo"})
public class MyConfiguration {
@Bean
StartTestService getDemoService() {
return new StartTestService();
}
}
3、编写业务代码,我这里写了一个简单的接口
controller层代码
package com.bobo.controller;
import com.bobo.service.StartTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author GBo
* @date 2024/3/5 15:11
*/
@RestController
public class StartTestController {
@Autowired
private StartTestService startTestService;
@GetMapping("/startTest")
public String findForPage() {
return startTestService.getSystemTime();
}
}
serveice层代码
package com.bobo.service;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* @author GBo
* @date 2024/3/5 14:36
*/
@Service
public class StartTestService {
public String getSystemTime() {
return "来自StartTestService的信息:" + new Date().toString();
}
}
4、指定自动装配
这是非常重要的一步,至此开发的业务代码就结束了,但是还有一个最重要的步骤就是指定 MyConfiguration 类作为自动装配类。
在 src/main/resources/META-INF目录下新建 spring.factories 文件,输入以下内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.bobo.config.MyConfiguration
这行代码的意思就是将 MyConfiguration设置为自动装配类,在 SpringBoot 工程启动时会去扫描 MATE-INF 文件夹的 spring.factories 文件,并加载这个文件中指定的类,启动自动装配功能。
5、发布&测试
使用maven install 命令,自定义的 starter 就可以作为一个依赖被引用了
6、在新的工程中,在 pom 文件中加入如下的依赖,就可以使用starter中的功能了
<dependency>
<groupId>org.example</groupId>
<artifactId>test-spring-boot-start</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
7、 接口测试
至此,我们已经完成了一个简单的 SpringBoot Start 的开发了。