Spirngboot-自定义Starter
一.Spring Boot Starter简介
Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件)进行自动配置。使用者只需要依赖相应功能的Starter,无需做过多的配置和依赖,Spring Boot就能自动扫描并加载相应的模块。
例如在Maven的依赖中加入spring-boot-starter-web就能使项目支持Spring MVC,并且Spring Boot还为我们做了很多默认配置,无需再依赖spring-web、spring-webmvc等相关包及做相关配置就能够立即使用起来。
二.Starter的开发步骤
编写Starter非常简单,与编写一个普通的Spring Boot应用没有太大区别,总结如下:
1.新建Maven项目,在项目的POM文件中定义使用的依赖;
2.新建配置类,写好配置项和默认的配置值,指明配置项前缀;
3.新建自动装配类,使用@Configuration和@Bean来进行自动装配;
4.新建spring.factories文件,指定Starter的自动装配类;
三.Starter的开发示例
下面,我就以创建一个自动配置来讲一下各个步骤及细节。
1.新建Maven项目,在项目的POM文件中定义使用的依赖。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
2.新建配置类,写好配置项和默认的配置值,指明配置项前缀。
@ConfigurationProperties("example.service")
public class StarterServiceProperties {
private String config;
private boolean enabled;
public void setConfig(String config) {
this.config = config;
}
public String getConfig() {
return config;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
指定配置项前缀为
example.service
,各配置项均有默认值,默认值可以通过模块使用者的配置文件进行覆盖。
3.新建自动装配类,使用@Configuration
和@Bean
来进行自动装配。
@Configuration
@EnableConfigurationProperties(StarterServiceProperties.class)
public class StarterAutoConfigure {
/***
* 注意:构建SpringBoot项目时候会自动增加plugin 工具,starter 不需要boot启动类
* 如果install 时报错和工具相关,需要删除plugin相关配置
*/
@Autowired
private StarterServiceProperties properties;
@Bean
@ConditionalOnMissingBean(StarterService.class)
@ConditionalOnProperty(prefix = "example.service", value = "enabled", havingValue = "true")
StarterService starterService (){
StarterService starterService = new StarterService();
starterService.setConfig(properties.getConfig());
return starterService;
}
}
4.新建spring.factories文件,指定Starter的自动装配类。
# 配置自动注入的类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.bootstarter.StarterAutoConfigure
spring.factories文件位于resources/META-INF目录下,需要手动创建;
org.springframework.boot.autoconfigure.EnableAutoConfiguration
后面的类名说明了自动装配类,如果有多个 ,则用逗号分开;
使用者应用(SpringBoot)在启动的时候,会通过org.springframework.core.io.support.SpringFactoriesLoader
读取classpath下每个Starter的spring.factories文件,加载自动装配类进行Bean的自动装配;
至此,整个Starter开发完毕,Deploy到中央仓库或Install到本地仓库后即可使用
四.Starter的使用
1.创建Maven项目,依赖刚才发布的es-starter包。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 依赖自定义starter -->
<dependency>
<groupId>com</groupId>
<artifactId>boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
只需依赖刚才开发的es-starter即可
2.根据要求进行配置
# starter 配置文件
example.service.config = abc-des-dde,SSS-DRS-RE,SDR-SDFR-XXX
example.service.enabled = false
3.编写应用程序启动类。
// 根据example.service.enabled 参数配置是否进行自动装配
@Component
@ConditionalOnExpression("${example.service.enabled:true}")
public class ServiceTest implements ApplicationRunner {
@Autowired
private StarterService starterService;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(Arrays.toString(starterService.split(",")));
}
}
5.运行程序测试
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
2019-08-29 14:28:27.061 INFO 9844 --- [ main] com.demo.BootDemoApplication : Starting BootDemoApplication on Kerwin with PID 9844 (C:\Users\Administrator\Desktop\Codes\KerwinBoots\boot-demo\target\classes started by Administrator in C:\Users\Administrator\Desktop\Codes\KerwinBoots)
2019-08-29 14:28:27.064 INFO 9844 --- [ main] com.demo.BootDemoApplication : No active profile set, falling back to default profiles: default
2019-08-29 14:28:28.317 INFO 9844 --- [ main] com.demo.BootDemoApplication : Started BootDemoApplication in 1.733 seconds (JVM running for 2.975)
[abc-des-dde, SSS-DRS-RE,SDR-SDFR-XXX]
运行程序,观察控制台输出: 源码可见: https://github.com/kkzhilu/KerwinBoots | boot_starter 分支
Spirngboot-自定义Starter的更多相关文章
- SpringBoot之旅第六篇-启动原理及自定义starter
一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...
- Spring Boot 自定义 starter
一.简介 SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应 ...
- java框架之SpringBoot(10)-启动流程及自定义starter
启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...
- SpringBoot应用篇(一):自定义starter
一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...
- SpringBoot第十六篇:自定义starter
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言 这一段时间 ...
- 小代学Spring Boot之自定义Starter
想要获取更多文章可以访问我的博客 - 代码无止境. 上一篇小代同学在Spring Boot项目中配置了数据源,但是通常来讲我们访问数据库都会通过一个ORM框架,很少会直接使用JDBC来执行数据库操作的 ...
- (springboot)自定义Starter
要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...
- SpringBoot自定义starter及自动配置
SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...
- 对照谈-官方spring-boot-starter和自定义starter异同分析
在前面我讲用spring-boot-starter-mail发邮件的时候,我侧重看的是spring boot发邮件的便利性,今天,我们聊下另外一个方面,spring-boot-starter自身的结构 ...
- SpringBoot自定义Starter实现
自定义Starter: Starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦.Starter 提供了一种开箱即用的理念,其中核心就是springboot的自动配置原理相 ...
随机推荐
- Project Loom:Reactive模型和协程进行时(翻译)
Java 15将发布Project Loom的第一个版本.我相信这将改变JVM.在这篇文章中,我想深入探讨一下导致我相信这一点的原因. 首先,我们需要了解核心问题.然后,我将尝试描述以前的技术如何解决 ...
- Python数据可视化实战:实时更新海外疫情数据,实现数据可视化
前言 我国的疫情已经得到了科学的控制,开始了全面的复工复产,但是国外的疫情却“停不下来”.国外现在可谓就是处于水深火热当中啊,病毒极强的传染性,导致了许多的人都“中招”了,我国已经全面复工复产了,人大 ...
- 阿里巴巴--mysql中Mysql模糊查询like效率,以及更高效的写法
在使用msyql进行模糊查询的时候,很自然的会用到like语句,通常情况下,在数据量小的时候,不容易看出查询的效率,但在数据量达到百万级,千万级的时候,查询的效率就很容易显现出来.这个时候查询的效率就 ...
- 入门大数据---HDFS-API
第一步:创建一个新的项目 并导入需要的jar包 公共核心包 公共依赖包 hdfs核心包 hdfs依赖包 第二步:将Linux中hadoop的配置文件拷贝到项目的src目录下 第三步:配置windows ...
- 【Spring注解驱动开发】BeanPostProcessor在Spring底层是如何使用的?看完这篇我懂了!!
写在前面 在<[String注解驱动开发]面试官再问你BeanPostProcessor的执行流程,就把这篇文章甩给他!>一文中,我们详细的介绍了BeanPostProcessor的执行流 ...
- 通俗易懂的阿里Sentinel源码分析:如何向控制台发送心跳包?
源码分析 public class Env { public static final Sph sph = new CtSph(); static { // 在Env类的静态代码块中, // 触发了一 ...
- 在 XUnit 中使用依赖注入
在 XUnit 中使用依赖注入 Intro 之前写过一篇 xunit 的依赖注入相关的文章,但是实际使用起来不是那么方便 今天介绍一个基于xunit和微软依赖注入框架的"真正"的依 ...
- Spring Boot注解大全,一键收藏了!
本文首发于微信公众号[猿灯塔],转载引用请说明出处 今天是猿灯塔“365天原创计划”第5天. 今天呢!灯塔君跟大家讲: Spring Boot注解大全 一.注解(annotations)列表 @Spr ...
- JVM源码分析之JVM启动流程
原创申明:本文由公众号[猿灯塔]原创,转载请说明出处标注 “365篇原创计划”第十四篇. 今天呢!灯塔君跟大家讲: JVM源码分析之JVM启动流程 前言: 执行Java类的main方法,程序就能运 ...
- linux磁盘容量不足的处理方案
在虚机上安装memcached时,突然发现磁盘空间不足. df -h 发现,磁盘一共12G,原来是新申请的虚机,磁盘分区没有挂载上. fdisk -l 查看磁盘,发现有 /dev/vdb1 /dev/ ...