Spring 全家桶之 Spring Boot 2.6.4(十)- 自定义Starter

简介: Spring 全家桶之 Spring Boot 2.6.4(十)- 自定义Starter

一、Spring Boot中的Starters

Spring Boot官网中关于Starters的介绍

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.

Starters既启动器是一组方便的依赖项描述符,在项目中应用这些Starters。您可以获得所需的所有Spring和相关技术的一站式服务,而无需查找示例代码和复制-粘贴大量依赖描述符。例如,如果您想开始使用Spring和JPA进行数据库访问,请在您的项目中包含Spring -boot-starter-data-jpa依赖项。

二、自定义Staters

Spring Boot官网中关于自定义Starters的介绍 7.9.5. Creating Your Own Starter

自定义Starter首先要确定场景的依赖,然后自定义Starter会用到以下注解或者配置。需要说明的是启动器是一个空的JAR文件,仅仅提供辅助性的依赖管理,这些依赖可能用于自动装配或者其他类库。

自定义Starter需要遵循一些命名约定

  • Spring Boot 官方Starter的前缀名为”spring-boot-starter-“,命名为“spring-boot-starter-模块名”,如spring-boot-starter-web、spring-boot-starter-jdbc
  • 第三方的Starter后缀名为“-spring-boot-starter”,命名为“模块名-spring-boot-starter”,如”mybatis-spring-boot-starter、druid-spring-boot-starter

首先创建一个Empty Project,将会在这个Project中创建Starter启动器工程lilith-spring-boot-starter和autoconfigure自动配置工程lilith-spring-boot-starter-autconfigure,lilith-spring-boot-starter启动器是一个空的JAR文件,仅仅提供辅助性的依赖管理。

接着在这个空工程里面添加Module,创建一个Maven工程作为启动器Starter lilith-spring-boot-starter。

然后再创建Spring Boot工程作为自动配置类lilith-spring-boot-starter-autconfigure,使用Spring Initializr工具创建

image.png

image.png

至此在这个Empty Project中共创建两个Module,分别是lilith-spring-boot-starter和lilith-spring-boot-starter-autconfigure

image.png

第一个是启动器,第二个是负责自动配置的模块

首先在启动器Starter中引入自动配置模块的依赖,在lilith-spring-boot-starter模块的pom.xml文件中增加

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <optional>true</optional>
</dependency>
复制代码

而lilith-spring-boot-starter-autoconfigure模块主要用来做自动配置,所以该模块下的主程序类以及配置文件和测试包可以删除,在pom.xml文件中只需要引入spring-boot-starter依赖即可,spring-boot-starter是所有starter的基本配置

自定义starter的需求是通过引入starter可以使用该starter中的Lilith类,使用该类可以通过配置打印出不同语言的”Hallo Lilith“

Lilith类代码,包含了一个LilithProperties类,用来定义配置项,hallo方法用来输出文本

public class Lilith {
    private LilithProperties lilithProperties;
    public LilithProperties getLilithProperties() {
        return lilithProperties;
    }
    public void setLilithProperties(LilithProperties lilithProperties) {
        this.lilithProperties = lilithProperties;
    }
    public String hallo(String name){
        return lilithProperties.getLanguages() + " " + name;
    }
}
复制代码

接着需要定义一个配置项的类LilithProperties,使用@ConfigurationProperties标记配置的prefix

@ConfigurationProperties(prefix = "lilith")
@Component
public class LilithProperties {
    // 语种
    private String languages;
    public String getLanguages() {
        return languages;
    }
    public void setLanguages(String languages) {
        this.languages = languages;
    }
}
复制代码

增加一个自动配置类LilithAutoConfiguration

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(LilithProperties.class)
public class LilithAutoConfiguration {
    @Autowired
    private LilithProperties lilithProperties;
    @Bean
    public Lilith lilith(){
        Lilith lilith = new Lilith();
        lilith.setLilithProperties(lilithProperties);
        return lilith;
    }
}
复制代码

resources目录下创建一个文件夹META-INF/spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lilith.starter.autoconfigure.LilithAutoConfiguration
复制代码

左侧目录结构如下

image.png

自动配置模块完成,自动配置类会往容器中添加Lilith类,Lilith类中用到的属性与LilithProperties类中的属性绑定

image.png

然后install到本地的maven仓库中

三、测试自定义的Starter

使用Spring Initializr工具创建一个新的工程spring-boot-lilith,选择Spring Web依赖即可,再引入自定义的starter,在pom.xml文件中添加以下依赖

<dependency>
    <groupId>com.lilith.starter</groupId>
    <artifactId>lilith-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
复制代码

新建controller包,增加HalloController

@RestController
public class HalloController {
    @Autowired
    private Lilith lilith;
    @GetMapping("/hallo")
    public String hallo(){
        return lilith.hallo("Lilith");
    }
}
复制代码

接着在application.properties中进行配置

lilith.languages=Buon Giorno
复制代码

启动应用,在浏览器输入 http://localhost:8080/hallo

image.png

页面上成功显示了意大利语的你好 Lilith

类似的例子还有阿里巴巴的Druid数据源以及MyBatis Plus,在Druid Starter出现之前都是通过书写配置类的方式既通过@Configuration和@Bean注解将Druid的DataSource导入到容器中,通过引入Stater的方式就可以省略配置类。


相关文章
|
8月前
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
8月前
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
8月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
738 2
|
9月前
|
人工智能 Java 机器人
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
Spring AI Alibaba集成Ollama,基于Java构建本地大模型应用,支持流式对话、knife4j接口可视化,实现高隐私、免API密钥的离线AI服务。
7031 2
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
存储 JSON Java
927 0
|
9月前
|
监控 安全 Java
使用 @HealthEndpoint 在 Spring Boot 中实现自定义健康检查
Spring Boot 通过 Actuator 模块提供了强大的健康检查功能,帮助开发者快速了解应用程序的运行状态。默认健康检查可检测数据库连接、依赖服务、资源可用性等,但在实际应用中,业务需求和依赖关系各不相同,因此需要实现自定义健康检查来更精确地监控关键组件。本文介绍了如何使用 @HealthEndpoint 注解及实现 HealthIndicator 接口来扩展 Spring Boot 的健康检查功能,从而提升系统的可观测性与稳定性。
651 0
使用 @HealthEndpoint 在 Spring Boot 中实现自定义健康检查
|
9月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
2872 0
|
10月前
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
1411 3