SpringBoot Starter缘起
SpringBoot通过SpringBoot Starter零配置自动加载第三方模块,只需要引入模块的jar包不需要任何配置就可以启用模块,遵循约定大于配置的思想。
那么如何编写一个SpringBoot Starter呢?我们需要考虑如下几个问题:
- 如何让SpringBoot发现我们编写的模块?
- 如何让模块读取SpringBoot的配置文件?
- 如果用户没有在配置文件中配置必要的配置项,如何默认禁用模块?
- 如何让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缘起的更多相关文章
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第7节 SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf_28..SpringBoot Starter讲解
笔记 1.SpringBoot Starter讲解 简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-b ...
- SpringBoot Starter机制 - 自定义Starter
目录 前言 1.起源 2.SpringBoot Starter 原理 3.自定义 Starter 3.1 创建 Starter 3.2 测试自定义 Starter 前言 最近在学习Sp ...
- 自定义springboot - starter 实现日志打印,并支持动态可插拔
1. starter 命名规则: springboot项目有很多专一功能的starter组件,命名都是spring-boot-starter-xx,如spring-boot-starter-loggi ...
- 从头带你撸一个Springboot Starter
我们知道 SpringBoot 提供了很多的 Starter 用于引用各种封装好的功能: 名称 功能 spring-boot-starter-web 支持 Web 开发,包括 Tomcat 和 spr ...
- SpringBoot starter 作用在什么地方?
依赖管理是所有项目中至关重要的一部分.当一个项目变得相当复杂,管理依赖会成为一个噩梦,因为当中涉及太多 artifacts 了. 这时候 SpringBoot starter 就派上用处了.每一个 s ...
- OpenFeign封装为springboot starter
OpenFeign是什么 随着业务的增多,我们的单体应用越来越复杂,单机已经难以满足性能的需求,这时候出现了分布式.分布式通讯除了RPC, REST HTTP请求是最简单的一种方式.OpenFeign ...
- SpringBoot - Starter
If you work in a company that develops shared libraries, or if you work on an open-source or commerc ...
- Springboot 系列(十五)如何编写自己的 Springboot starter
1. 前言 Springboot 中的自动配置确实方便,减少了我们开发上的复杂性,那么自动配置原理是什么呢?之前我也写过了一篇文章进行了分析. Springboot 系列(三)Spring Boot ...
- 手写一个springboot starter
springboot的starter的作用就是自动装配.将配置类自动装配好放入ioc容器里.作为一个组件,提供给springboot的程序使用. 今天手写一个starter.功能很简单,调用start ...
随机推荐
- 解决使用 Eruda 绑定 dom 未在指定位置显示问题
前言 开发项目中,使用到 Eruda 打印控制台信息显示 文档:https://github.com/liriliri/eruda 安装 Eruda npm install eruda --save ...
- [游记]CSP 2021 J/S
这一次,也许是我的OI生涯的转折点了--能过,学习OI的时间就不会减少:但不能过,就会减少学习OI的时间-- 上午(S组) 6:00起床.去吃早餐,结果因为边喝粥边喝牛奶导致肚子疼.(我在这里劝大家, ...
- MySQL查询性能优化七种武器之链路追踪
MySQL优化器可以生成Explain执行计划,我们可以通过执行计划查看是否使用了索引,使用了哪种索引? 但是到底为什么会使用这个索引,我们却无从得知. 好在MySQL提供了一个好用的工具 - opt ...
- 造数字(数位DP)
题面 JZM 想要创造两个数字 x 和 y,它们需要满足 x or y = T,且Lx ≤ x ≤ Rx, Ly ≤ y ≤ Ry,JZM 想知道 x and y 有多少种可能的不同的取值.若有多组 ...
- idea中无法在@Test 之下使用Scanner
//如何解决idea中无法在 @Test 之下使用Scanner@Testpublic void testInsert(){ Scanner scanner = new Scanner(System. ...
- SpringBoot_事务总结
Springboot 事务 1. 打印SQL 日志的两种配置方式 [1]通过配置包的log等级来打印SQL日志,但这种不会打印出事务日志 logging.level.com.grady.mybatis ...
- 【FAQ】接入华为应用内支付服务常见问题解答
HMS Core应用内支付服务(In-App Purchases,IAP)为应用提供便捷的应用内支付体验和简便的接入流程.开发者的应用集成IAP SDK后,调用IAP SDK接口,启动IAP收银台,即 ...
- Mysql 实现 向上递归查找父节点并返回树结构
需求:通过mysql 8.0以下版本实现,一个人多角色id,一个角色对应某个节点menu_id,根节点的父节点存储为NULL, 向上递归查找父节点并返回树结构. 如果只有叶子,剔除掉; 如果只有根,只 ...
- [Python]-string-字符串
字符串是Python中很常用的数据类型,此处记录一些典型用法并随时更新. split()方法 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串. 两个参数st ...
- 聊聊单点登录(SSO)中的CAS认证
SSO介绍 背景 随着企业的发展,一个大型系统里可能包含 n 多子系统, 用户在操作不同的系统时,需要多次登录,很麻烦,我们需要一种全新的登录方式来实现多系统应用群的登录,这就是单点登录. web 系 ...