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 分支
最新文章
- java虚拟机运行时乱码问题
- 【Qt】2.3 使用Qt设计师来创建对话框
- JS图片延迟加载分析及简单的demo
- 【leetcode❤python】 111. Minimum Depth of Binary Tree
- qt外部数据传入实现动态的折线图绘制
- mac下Apache添加限速模块mod_bw
- (转) Linux的capability深入分析(2)
- 转:JavaScript函数式编程(一)
- 开启apache服务
- 系统自动生成ID(比UUID.radom().tostring()要好看)
- JS和OC交互的简单应用
- my new start
- 201521123066 《Java程序设计》 第二周学习总结
- 到底vuex是什么?
- 【BZOJ3531】旅行(树链剖分,线段树)
- asp.net core 排序过滤分页组件:sieve(2)表达式树的复习
- vscode 插件 与 技巧
- HTML中特殊符号
- flask请求异步执行(转载)
- MySQL DBA工作角色和职责介绍
热门文章
- Java8新特性之函数式接口
- Dubbo——SPI及自适应扩展原理
- Centos7 composer安装时 Warning: This development build of composer is over 60 days old. It is recommended to update it by running ";/usr/bin/composer self-update"; to get the latest version.
- brother b-PAC sdk开发包
- Java使用SQLServerBulKCopy实现批量插入SQLSqerver数据库
- cookie的介绍和使用
- Ubuntu16.06常见服务搭建
- elasticserach数据库深度分页查询的原理
- 弹性盒模型中flex-grow 和flex的区别
- JS判断 函数是否定义/变量是否定义