实战|如何自定义SpringBoot Starter?

简介: SpringBoot 项目就是由一个一个 Starter 组成的,一个 Starter 代表该项目的 SpringBoot 起步依赖,除了官方已有的 Starter,如果你需要将自己的项目支持 SpringBoot,那么就需要把它制作成一个 Starter。

我在「SpringBoot自动化配置源码分析」从源码的角度讲解了 SpringBoot 自动化配置的原理,知道了它最终要干的事情不过是读取 META-INF/spring.factories 中的自动化配置类而已。


SpringBoot 项目就是由一个一个 Starter 组成的,一个 Starter 代表该项目的 SpringBoot 起步依赖,除了官方已有的 Starter,如果你需要将自己的项目支持 SpringBoot,那么就需要把它制作成一个 Starter。这篇博客依据 SpringBoot 的自动化配置原理,开发一个属于自己的 Starter。


自定义 Starter



自动化配置需满足两个条件:


1.能够生成 Bean,并注册到 Bean 容器中;

2.能够自动配置项目所需要的配置。

在这里创建一个 spring-boot-starter-helloworld 项目作为例子,实现以上两点。


引入 SpringBoot 自动化配置依赖:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>1.5.9.RELEASE</version>
  </dependency>
</dependencies>


spring-boot-starter-helloworld 只是作为例子演示自定义 starter 的过程,实现的功能很简单就是创建一个 HelloworldService 的,并配置 sayHello() 方法打印的语句。


public class HelloworldService {
  private String words;
  private String getWords() {
    return words;
  }
  public void setWords(String words) {
    this.words = words;
  }
  public String sayHello() {
     return "hello, " + words;
  }
}


创建属性类,prefix = "helloworld"代表该项目在属性文件中配置的前缀,即可以在属性文件中通过 helloworld.words=springboot,就可以改变属性类字段 words 的值了。


@ConfigurationProperties(prefix = "helloworld")
public class HelloworldProperties {
  public static final String DEFAULT_WORDS = "world";
  private String words = DEFAULT_WORDS;
  public String getWords() {
    return words;
  }
  public void setWords(String words) {
    this.words = words;
  }
}


创建自动化配置类,这个相当于就是一个普通的 Java 配置类,可以在这里创建 Bean,并可获得与 application.properties 属性文件相对应的属性类的 Bean。


// 相当于一个普通的 java 配置类
@Configuration
// 当 HelloworldService 在类路径的条件下
@ConditionalOnClass({HelloworldService.class})
// 将 application.properties 的相关的属性字段与该类一一对应,并生成 Bean
@EnableConfigurationProperties(HelloworldProperties.class)
public class HelloworldAutoConfiguration {
  // 注入属性类
  @Autowired
  private HelloworldProperties hellowordProperties;
  @Bean
  // 当容器没有这个 Bean 的时候才创建这个 Bean
  @ConditionalOnMissingBean(HelloworldService.class)
  public HelloworldService helloworldService() {
    HelloworldService helloworldService = new HelloworldService();
    helloworldService.setWords(hellowordProperties.getWords());
    return helloworldService;
  }
}


在 META-INF 目录下创建 spring.factories,这个属性文件可重要啦,因为 SpringBoot 自动化配置最终就是要扫描 META-INF/spring.factories 来加载项目的自动化配置类。


# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.objcoding.starters.helloworld.HelloworldAutoConfiguration


引用 Starter



为了引入 starter,我在这里再创建一个 spring-boot-starter-helloworld-sample 项目。


添加 spring-boot-starter-helloworld 起步依赖:

<dependency>
  <groupId>com.objcoding</groupId>
  <artifactId>spring-boot-starter-helloworld</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>


在 application.properties 中添加属性:


helloworld.words=springboot


在 SpringBoot 主程序中 注入 helloworldService

@RestController
@SpringBootApplication
public class HelloworldApplication {
  @Autowired
  private HelloworldService helloworldService;
  @GetMapping("/")
  public String sayHello() {
    return helloworldService.sayHello();
  }
  public static void main(String[] args) {
    SpringApplication.run(HelloworldApplication.class, args);
  }
}


访问 http://localhost:8080/,打印以下结果:

640.png

Demo源码地址:https://github.com/objcoding/spring-boot-starter-tutorial



相关文章
|
并行计算 Java 数据处理
SpringBoot高级并发实践:自定义线程池与@Async异步调用深度解析
SpringBoot高级并发实践:自定义线程池与@Async异步调用深度解析
803 0
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
|
2月前
|
监控 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注册中心服务 构建商品
447 3
|
16天前
|
监控 Cloud Native Java
Spring Boot 3.x 微服务架构实战指南
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Spring Boot 3.x与微服务架构,探索云原生、性能优化与高可用系统设计。以代码为笔,在二进制星河中谱写极客诗篇。关注我,共赴技术星辰大海!(238字)
Spring Boot 3.x 微服务架构实战指南
|
1月前
|
消息中间件 Ubuntu Java
SpringBoot整合MQTT实战:基于EMQX实现双向设备通信
本教程指导在Ubuntu上部署EMQX 5.9.0并集成Spring Boot实现MQTT双向通信,涵盖服务器搭建、客户端配置及生产实践,助您快速构建企业级物联网消息系统。
373 1
|
7月前
|
缓存 NoSQL Java
基于SpringBoot的Redis开发实战教程
Redis在Spring Boot中的应用非常广泛,其高性能和灵活性使其成为构建高效分布式系统的理想选择。通过深入理解本文的内容,您可以更好地利用Redis的特性,为应用程序提供高效的缓存和消息处理能力。
588 79
|
5月前
|
监控 Java 调度
SpringBoot中@Scheduled和Quartz的区别是什么?分布式定时任务框架选型实战
本文对比分析了SpringBoot中的`@Scheduled`与Quartz定时任务框架。`@Scheduled`轻量易用,适合单机简单场景,但存在多实例重复执行、无持久化等缺陷;Quartz功能强大,支持分布式调度、任务持久化、动态调整和失败重试,适用于复杂企业级需求。文章通过特性对比、代码示例及常见问题解答,帮助开发者理解两者差异,合理选择方案。记住口诀:单机简单用注解,多节点上Quartz;若是任务要可靠,持久化配置不能少。
535 4
|
人工智能 自然语言处理 前端开发
SpringBoot + 通义千问 + 自定义React组件:支持EventStream数据解析的技术实践
【10月更文挑战第7天】在现代Web开发中,集成多种技术栈以实现复杂的功能需求已成为常态。本文将详细介绍如何使用SpringBoot作为后端框架,结合阿里巴巴的通义千问(一个强大的自然语言处理服务),并通过自定义React组件来支持服务器发送事件(SSE, Server-Sent Events)的EventStream数据解析。这一组合不仅能够实现高效的实时通信,还能利用AI技术提升用户体验。
805 2
|
6月前
|
缓存 安全 Java
深入解析HTTP请求方法:Spring Boot实战与最佳实践
这篇博客结合了HTTP规范、Spring Boot实现和实际工程经验,通过代码示例、对比表格和架构图等方式,系统性地讲解了不同HTTP方法的应用场景和最佳实践。
583 5
|
8月前
|
Java Spring
SpringBoot 实战 不同参数调用不同实现
本文介绍了如何在实际工作中根据不同的入参调用不同的实现,采用`map+enum`的方式实现优雅且严谨的解决方案。通过Spring Boot框架中的工厂模式或策略模式,避免了使用冗长的`if...else...`语句。文中详细展示了定义接口、实现类、枚举类以及控制器调用的代码示例,确保用户输入的合法性并简化了代码逻辑。
211 1
SpringBoot 实战 不同参数调用不同实现