来源:http://t.cn/Ai9li9fC

众所周知,Spring Boot由众多Starter组成,随着版本的推移Starter家族成员也与日俱增。在传统Maven项目中通常将一些层、组件拆分为模块来管理,以便相互依赖复用,在Spring Boot项目中我们则可以创建自定义Spring Boot Starter来达成该目的。

好,开始,先创建一个Maven项目并引入依赖,pom.xml如下,供参考~

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>example-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><!-- Import dependency management from Spring Boot --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.2.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
</project>

这里说下artifactId的命名问题,Spring 官方 Starter通常命名为spring-boot-starter-{name}如 spring-boot-starter-web, Spring官方建议非官方Starter命名应遵循{name}-spring-boot-starter的格式。

这里讲一下我们的Starter要实现的功能,很简单,提供一个Service,包含一个能够将字符串加上前后缀的方法String wrap(String word)

public class ExampleService {private String prefix;private String suffix;public ExampleService(String prefix, String suffix) {this.prefix = prefix;this.suffix = suffix;}public String wrap(String word) {return prefix + word + suffix;}}

前缀、后缀通过读取application.properties(yml)内的参数获得

@ConfigurationProperties("example.service")
public class ExampleServiceProperties {private String prefix;private String suffix;//省略 getter setter
}

重点,编写AutoConfigure

@Configuration
@ConditionalOnClass(ExampleService.class)
@EnableConfigurationProperties(ExampleServiceProperties.class)
public class ExampleAutoConfigure {@Autowiredprivate ExampleServiceProperties properties;@Bean@ConditionalOnMissingBean@ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true")ExampleService exampleService (){return  new ExampleService(properties.getPrefix(),properties.getSuffix());}}

解释下用到的几个和Starter相关的注解:

  • @ConditionalOnClass,当classpath下发现该类的情况下进行自动配置。

  • @ConditionalOnMissingBean,当Spring Context中不存在该Bean时。

  • @ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true"),当配置文件中example.service.enabled=true时。

更多相关注解,建议阅读官方文档该部分。

最后一步,在resources/META-INF/下创建spring.factories文件,内容供参考下面~

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autocinfigure.ExampleAutoConfigure

OK,完事,运行 mvn:install打包安装,一个Spring Boot Starter便开发完成了。如果你需要该Starter的源代码,点这里。


创建一个Spring Boot项目来 试试~

引入example-spring-boot-starter依赖

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

创建application.properties,进行配置

example.service.enabled=true
example.service.prefix=####
example.service.suffix=@@@@

创建一个简单的Spring Web Application,注入Starter提供的ExampleService看它能否正常工作~

@SpringBootApplication
@RestController
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Autowiredprivate ExampleService exampleService;@GetMapping("/input")public String input(String word){return exampleService.wrap(word);}}

启动Application,访问/input接口试试看~


总结下Starter的工作原理

  1. Spring Boot在启动时扫描项目所依赖的JAR包,寻找包含spring.factories文件的JAR包

  2. 根据spring.factories配置加载AutoConfigure

  3. 根据 @Conditional注解的条件,进行自动配置并将Bean注入Spring Context

快速开发一个自定义 Spring Boot Starter ,希望你也会相关推荐

  1. 自定义依赖注解无效_最详细的自定义Spring Boot Starter开发教程

    1.前言 随着Spring的日渐臃肿,为了简化配置.开箱即用.快速集成,Spring Boot 横空出世.目前已经成为 Java 目前最火热的框架了.平常我们用Spring Boot开发web应用.S ...

  2. Spring Boot快速开发利器:Spring Boot CLI

    Spring Boot CLI(Command Line Interface)是一个命令行工具,您可以用它来快速构建Spring原型应用.通过Spring Boot CLI,我们可以通过编写Groov ...

  3. 自定义 Spring Boot Starter

    一.引言 什么是Spring Boot Starter呢?我们直接来看看官网是怎么介绍的吧. Starters are a set of convenient dependency descripto ...

  4. Spring Boot - 手把手教小师妹自定义Spring Boot Starter

    文章目录 Pre 自定义starter的套路 步骤 命名规范 官方命名空间 自定义命名空间 实战 创建一个父maven项目:springboot_custome_starter 创建 两个Module ...

  5. SpringBoot快速开发利器:Spring Boot CLI

    今日推荐 推荐一个 Java 接口快速开发框架干掉Random:这个类已经成为获取随机数的王者Docker + Intellij IDEA,提升 10 倍生产力!笑出腹肌的注释,都是被代码耽误的诗人! ...

  6. 如何添加MySQL插件_如何开发一个自定义的MySQL插件

    MySQL自带了很多插件,比如半同步插件.审计插件.密码验证插件等等,甚至MySQL存储引擎也是以插件方式实现的.MySQL开放的插件接口,为开发者开发自定义插件提供了便利.本文将介绍如何快速开发一个 ...

  7. 从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建

    从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建 本文简介 为什么使用Spring Boot 搭建怎样一个环境 开发环境 导入快速启动 ...

  8. 一个项目有两个pom_实现一个Spring Boot Starter超简单,读 Starter 源码也不在话下...

    Spring Boot 对比 Spring MVC 最大的优点就是使用简单,约定大于配置.不会像之前用 Spring MVC 的时候,时不时被 xml 配置文件搞的晕头转向,冷不防还因为 xml 配置 ...

  9. 实现一个 Spring Boot Starter 原来如此简单,读 Starter 源码也不在话下

    我是风筝,公众号「古时的风筝」,一个在程序圈混迹多年,主业 Java,另外 Python.React 也玩儿的 6 的斜杠开发者.现已转行程序员鼓励师 Spring Cloud 系列文章已经完成,可以 ...

最新文章

  1. Oracle中的Union、Union All、Intersect、Minus 使用用法区别
  2. Windows 系统电脑开机速度加快
  3. 我的泰坦尼克数据分析
  4. bug death march
  5. Docker:使用本地卷和tmpfs挂载
  6. Intel 64/x86_64/IA-32/x86处理器 - SIMD指令集 - SSE扩展(12) - 预取指令与SFENCE指令
  7. 构造函数中的常见错误
  8. 机器学习:02 特征工程和决策树回归
  9. 2019年日本上班的你必须知道的社会保障和源泉所得税
  10. 计算机一级考试有填空题嘛,计算机一级考试填空题
  11. 文件和数据格式化~总结
  12. 他山之石 | 微信搜一搜中的智能问答技术
  13. 行走在数据库上的行癫(二)
  14. 微信3.7.6.29 pc版无法使用fiddler抓小程序包
  15. Unity3D 2018 3.0新手入门
  16. php 时间加法函数_PHP 日期时间函数的高级应用技巧
  17. 多商户商城系统功能拆解13讲-平台端会员管理
  18. 21届毕业生毕业一年内的状态
  19. 股票历史数据-股票价格查询,股票历史交易价格查询
  20. 关于CH376拷贝U盘文件速度的测试比较

热门文章

  1. 大话设计模式(七 工厂不好用了?)
  2. rman备份恢复总结
  3. iOS 生日字符串转化年龄
  4. CentOS 6.7安装Storm 0.9.7
  5. mysql 获取当前日期及格式化
  6. 近期北京动点软件发现XXX公司盗用我公司WPF项目案例
  7. OpenGL 关于旧版glut和新版本glfw和glad的环境配置
  8. 【golang】Go语言学习-select用法
  9. hadoop centos 安装
  10. linux系统调用理解之摘录(1)