SpringBoot通过SpringBoot Starter零配置自动加载第三方模块,只需要引入模块的jar包不需要任何配置就可以启用模块,遵循约定大于配置的思想。

那么如何编写一个SpringBoot Starter呢?我们需要考虑如下几个问题:

  1. 如何让SpringBoot发现我们编写的模块?
  2. 如何让模块读取SpringBoot的配置文件?
  3. 如果用户没有在配置文件中配置必要的配置项,如何默认禁用模块?
  4. 如何让SpringBoot知道模块有哪些配置项目,方便用户配置配置文件?

一.模块的发现

由于SpringBoot默认的包扫描路径是主程序所在包及其下面的所有子包里面的组件,引入的jar包下的类是不会被扫描的。那么如何去加载Starter的配置类呢?

SpringBoot约定会扫描Starter jar包META-INF目录下的spring.factories文件,只需要在spring.factories文件里配置要加载的类就可以了。下面的是Arthas的配置文件(arthas-spring-boot-starter-3.6.3.jar,Arthas是Alibaba开源的Java诊断工具。)。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.alibaba.arthas.spring.ArthasConfiguration,\ com.alibaba.arthas.spring.endpoints.ArthasEndPointAutoConfiguration

这样SpringBoot在启动的时候就会自动加载这些类存放到容器中管理。

二.模块读取SpringBoot的配置文件

ArthasConfiguration的源码:

@ConditionalOnProperty(
name = {"spring.arthas.enabled"},
matchIfMissing = true
)
@EnableConfigurationProperties({ArthasProperties.class})
public class ArthasConfiguration {
@ConfigurationProperties(
prefix = "arthas"
)
@ConditionalOnMissingBean(
name = {"arthasConfigMap"}
)
@Bean
public HashMap<String, String> arthasConfigMap() {
return new HashMap();
} @ConditionalOnMissingBean
@Bean
public ArthasAgent arthasAgent(@Autowired @Qualifier("arthasConfigMap") Map<String, String> arthasConfigMap, @Autowired ArthasProperties arthasProperties) throws Throwable {
arthasConfigMap = StringUtils.removeDashKey(arthasConfigMap);
ArthasProperties.updateArthasConfigMapDefaultValue(arthasConfigMap);
String appName = this.environment.getProperty("spring.application.name");
if (arthasConfigMap.get("appName") == null && appName != null) {
arthasConfigMap.put("appName", appName);
} Map<String, String> mapWithPrefix = new HashMap(arthasConfigMap.size());
Iterator var5 = arthasConfigMap.entrySet().iterator(); while(var5.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry)var5.next();
mapWithPrefix.put("arthas." + (String)entry.getKey(), entry.getValue());
} ArthasAgent arthasAgent = new ArthasAgent(mapWithPrefix, arthasProperties.getHome(), arthasProperties.isSlientInit(), (Instrumentation)null);
arthasAgent.init();
logger.info("Arthas agent start success.");
return arthasAgent;
}
}

我们发现类上面两个注解EnableConfigurationProperties和ConditionalOnProperty。这两个注解的作用如下:

  • EnableConfigurationProperties用来指定要加载的配置类,配置类用来加载SpringBoot的配置文件,SpringBoot配置文件中可以指定Arthas的启动参数。如果你不需要任何参数,则可以不指定EnableConfigurationProperties。
@ConfigurationProperties(
prefix = "arthas"
)
public class ArthasProperties {
private String ip;
private int telnetPort;
private int httpPort;
private String tunnelServer;
private String agentId;
private String appName;
private String statUrl;
}
  • ConditionalOnProperty通过读取SpringBoot配置文件的指定参数判断是否启用组件,如果判断为False,ArthasConfiguration里的Bean就不会被加载到容器中,即组件的开关。Arthas读取spring.arthas.enabled来判断是否加载组件。如果你想默认启动,没有开关,则可以不指定ConditionalOnProperty。

三.编写自己的Starter

我们写个简单的Starter,不加载配置文件,并且默认启动的Starter。IDE用的是是IDEA社区版。

3.1创建一个SpringBootStarter项目

3.1.1 新建项目,选择使用Maven构建。

3.1.2 然后创建spring.factories文件和配置类。

3.1.3 spring.factories写入配置类的全称。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.bjgoodwil.SpringBootStarterConfiguration

3.1.4 Maven打包

使用Maven的site命令打包jar到自己的本地仓库中。

3.2在SpringBoot项目中引入jar

打开一个SpringBoot项目,在pom文件中引入Jar(直接拷贝Starter pom文件中的参数),启动SpringBoot项目,控制台就会打印配置类的打印语句。

<!--自定义Starter-->
<dependency>
<groupId>com.bjgoodwill</groupId>
<artifactId>springbootstarter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

SpringBoot Starter缘起的更多相关文章

  1. 小D课堂 - 零基础入门SpringBoot2.X到实战_第7节 SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf_28..SpringBoot Starter讲解

    笔记 1.SpringBoot Starter讲解     简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-b ...

  2. SpringBoot Starter机制 - 自定义Starter

    目录 前言 1.起源 2.SpringBoot Starter 原理 3.自定义 Starter 3.1 创建 Starter 3.2 测试自定义 Starter 前言         最近在学习Sp ...

  3. 自定义springboot - starter 实现日志打印,并支持动态可插拔

    1. starter 命名规则: springboot项目有很多专一功能的starter组件,命名都是spring-boot-starter-xx,如spring-boot-starter-loggi ...

  4. 从头带你撸一个Springboot Starter

    我们知道 SpringBoot 提供了很多的 Starter 用于引用各种封装好的功能: 名称 功能 spring-boot-starter-web 支持 Web 开发,包括 Tomcat 和 spr ...

  5. SpringBoot starter 作用在什么地方?

    依赖管理是所有项目中至关重要的一部分.当一个项目变得相当复杂,管理依赖会成为一个噩梦,因为当中涉及太多 artifacts 了. 这时候 SpringBoot starter 就派上用处了.每一个 s ...

  6. OpenFeign封装为springboot starter

    OpenFeign是什么 随着业务的增多,我们的单体应用越来越复杂,单机已经难以满足性能的需求,这时候出现了分布式.分布式通讯除了RPC, REST HTTP请求是最简单的一种方式.OpenFeign ...

  7. SpringBoot - Starter

    If you work in a company that develops shared libraries, or if you work on an open-source or commerc ...

  8. Springboot 系列(十五)如何编写自己的 Springboot starter

    1. 前言 Springboot 中的自动配置确实方便,减少了我们开发上的复杂性,那么自动配置原理是什么呢?之前我也写过了一篇文章进行了分析. Springboot 系列(三)Spring Boot ...

  9. 手写一个springboot starter

    springboot的starter的作用就是自动装配.将配置类自动装配好放入ioc容器里.作为一个组件,提供给springboot的程序使用. 今天手写一个starter.功能很简单,调用start ...

随机推荐

  1. Spring源码 07 IOC refresh方法2

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  2. 【游记】CSP 2021 J2

    这次是第一次参加CSP的复赛,所以考的就很LJ. \(DAY-\infty\) 到 \(DAY-14\) 知道了自己苟过了初赛,像个SB一样. (我初赛66分,旁边那位63.5,cao着线过去的) \ ...

  3. 解决eclipse中的Java文件,使用idea打开的乱码问题

    吐槽: 在克隆一些Github上面资源的时候,使用idea打开,会出现乱码的情况 而使用eclipse打开,这种情况就会消失.「是因为eclipse使用的是GBK编码,idea使用的是utf-8」 这 ...

  4. flutter系列之:Material主题的基础-MaterialApp

    简介 为了简化大家的使用,虽然flutter推荐所有的widget都有自己来进行搭建,但是在大框架上面,flutter提供了Material和Cupertino两种主题风格的Widgets集合,大家可 ...

  5. Linux虚拟机启动报错operating system not found解决步骤

    此报错为硬盘上的启动代码丢失 实验准备步骤 1) 准备: dd if=/dev/zero of=/dev/nvme0n1 bs=446 count=1 2) 系统启动报错截图 修复步骤如下 第一步:选 ...

  6. 如何将原生微信小程序页面改成原生VUE框架的H5页面

    项目背景: 公司为了实现小程序与H5页面共同覆盖,全面推广.特此想将已有的小程序进行快速改造上线(二周内),研发出H5版本.目前公司前端技术较为薄弱,现有的技术解决方案还停留在JSP. 问题: 如何将 ...

  7. KingbaseES 数据库删除功能组件

      关键字: KingbaseES.卸载.删除功能   一.安装后检查 在安装完成后,可以通过以下几种方式进行安装正确性验证: 1. 查看安装日志,确认没有错误记录; 2. 查看开始菜单: 查看应用程 ...

  8. Java 多线程:并发编程的三大特性

    Java 多线程:并发编程的三大特性 作者:Grey 原文地址: 博客园:Java 多线程:并发编程的三大特性 CSDN:Java 多线程:并发编程的三大特性 可见性 所谓线程数据的可见性,指的就是内 ...

  9. ProxySQL 配置MySQL节点

    转载自:https://www.jianshu.com/p/ca1b78b5d615 可以在mysql_servers表和mysql_replication_hostgroups表(可选)中配置后端的 ...

  10. 使用Gitlab的CI/CD功能自动化推送docker镜像到Nexus仓库出现的问题

    在服务器中可以直接使用命令行登录,推送docker镜像等 但是在使用Gitlab的CI/CD功能中,gitlab-ci.yml文件执行过程中出现如下错误: 原因分析: 服务器上之前使用命令行登陆过Ne ...