自定义 Spring Boot Starter
关于Starter
Spring Boot秉承“约定大于配置”的开发方式,使得我们基于Spring Boot开发项目的效率变得十分高。相信使用过Spring Boot的小伙伴都会发现,当我们要用到某个Spring提供的组件时,只需要在
pom.xml文件中添加该组件的starter依赖就能集成到项目中。例如,在
pom.xml文件中添加spring-boot-starter-web依赖,就能让项目整合Spring MVC的功能。并且在最简使用下几乎不需要进行任何的配置,而以往想要集成Spring MVC,不仅要添加一堆类似于spring-web、spring-webmvc等相关依赖包,以及完成许多繁杂的配置才能够实现集成。这是因为starter里已经帮我们整合了各种依赖包,避免了依赖包缺失或依赖包之间出现版本冲突等问题。以及完成了许多基础配置和自动装配,让我们可以在最简使用下,跳过绝大部分的配置,从而达到开箱即用的效果。这也是Spring Boot实现“约定大于配置”的核心之一。
动手开发一个Starter
通过以上的描述,我们可以简单地将starter看作是对一个组件功能粒度较大的模块化封装,包括了所需依赖包的整合及基础配置和自动装配等。
这里说下
artifactId的命名问题,Spring 官方 Starter通常命名为spring-boot-starter-{name}如spring-boot-starter-web, Spring官方建议非官方Starter命名应遵循{name}-spring-boot-starter的格式。
除了Spring官方提供的starter外,我们自己也可以根据业务开发一个starter。例如,当项目积累到一定程度时,我们可以将一些通用功能下沉为一个starter。而开发一个starter也很简单,只需要以下步骤:
- 新建一个Maven项目,在pom.xml文件中定义好所需依赖;
- 新建配置类,写好配置项和默认值,使用
@ConfigurationProperties指明配置项前缀; - 新建自动装配类,使用
@Configuration和@Bean来进行自动装配; - 新建
spring.factories文件,用于指定自动装配类的路径; - 将starter安装到maven仓库,让其他项目能够引用;
接下来,以封装一个用于操作redis的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>redis-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-starter-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
<!-- gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
</project>
第二步,新建一个属性配置类,写好配置项和默认值。并使用@ConfigurationProperties指明配置项前缀,用于加载配置文件对应的前缀配置项:
package com.example.starter.demo.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 属性配置类,用于加载配置文件对应的前缀配置项
**/
@Data
@ConfigurationProperties("demo.redis")
public class RedisProperties {
private String host = "127.0.0.1";
private int port = 6379;
private int timeout = 2000;
private int maxIdle = 5;
private int maxTotal = 10;
private long maxWaitMillis = 10000;
private String password;
}
编写一个简单的redis操作工具,代码如下:
package com.example.starter.demo.component;
import com.google.gson.Gson;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* redis 操作组件
**/
@Slf4j
@RequiredArgsConstructor
public class RedisComponent {
private final JedisPool jedisPool;
/**
* get value with key
*/
public <T> T get(String key, Class<T> clazz) {
try (Jedis resource = jedisPool.getResource()) {
String str = resource.get(key);
return stringToBean(str, clazz);
}
}
/**
* set value with key
*/
public <T> boolean set(String key, T value, int expireSeconds) {
try (Jedis resource = jedisPool.getResource()) {
String valueStr = beanToString(value);
if (valueStr == null || valueStr.length() == 0) {
return false;
}
if (expireSeconds <= 0) {
resource.set(key, valueStr);
} else {
resource.setex(key, expireSeconds, valueStr);
}
return true;
}
}
private <T> T stringToBean(String str, Class<T> clazz) {
Gson gson = new Gson();
return gson.fromJson(str, clazz);
}
private <T> String beanToString(T value) {
Gson gson = new Gson();
return gson.toJson(value);
}
}
第三步,新建自动装配类,使用@Configuration和@Bean来实现对JedisPool和RedisComponent的自动装配;
package com.example.starter.demo.configuration;
import com.example.starter.demo.component.RedisComponent;
import com.example.starter.demo.properties.RedisProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* 自动装配类
**/
@Slf4j
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfiguration {
private final RedisProperties properties;
@Bean
// 表示当Spring容器中没有JedisPool类的对象时,才调用该方法
@ConditionalOnMissingBean(JedisPool.class)
public JedisPool jedisPool() {
log.info("redis connect string: {}:{}", properties.getHost(), properties.getPort());
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(properties.getMaxIdle());
jedisPoolConfig.setMaxTotal(properties.getMaxTotal());
jedisPoolConfig.setMaxWaitMillis(properties.getMaxWaitMillis());
String password = properties.getPassword();
if (password == null || password.length() == 0) {
return new JedisPool(jedisPoolConfig, properties.getHost(),
properties.getPort(), properties.getTimeout());
}
return new JedisPool(jedisPoolConfig, properties.getHost(),
properties.getPort(), properties.getTimeout(), properties.getPassword());
}
@Bean
@ConditionalOnMissingBean(RedisComponent.class)
public RedisComponent redisComponent(JedisPool jedisPool){
return new RedisComponent(jedisPool);
}
}
第四步,在项目的resources目录下新建一个META-INF目录,并在该目录下新建spring.factories文件。如下图所示:

在spring.factories文件里指定自动装配类的路径:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.starter.demo.configuration.RedisConfiguration
若需要指定多个自动装配类的路径,则使用逗号分隔。如下示例:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.starter.demo.configuration.DemoConfiguration,\
com.example.starter.demo.configuration.RedisConfiguration
Tips:spring.factories支持配置的key如下:
org.springframework.context.ApplicationContextInitializer
org.springframework.context.ApplicationListener
org.springframework.boot.autoconfigure.AutoConfigurationImportListener
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter
org.springframework.boot.autoconfigure.EnableAutoConfiguration
org.springframework.boot.diagnostics.FailureAnalyzer
org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider
org.springframework.boot.env.EnvironmentPostProcessor
org.springframework.boot.SpringApplicationRunListener
org.springframework.boot.SpringBootExceptionReporter
org.springframework.beans.BeanInfoFactory
org.springframework.boot.env.PropertySourceLoader
org.springframework.data.web.config.SpringDataJacksonModules
org.springframework.data.repository.core.support.RepositoryFactorySupport
最后install这个maven项目,命令如下:
mvn clean install
如果使用的开发工具是IDEA的话就比较简单,只需要双击一下install即可:

使用Starter
在任意一个Spring Boot项目的pom.xml文件中添加如下依赖:
<dependency>
<groupId>com.example</groupId>
<artifactId>redis-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
在项目的application.yml中添加如下配置项来覆盖默认配置,若默认配置已符合需求则可以省略这一步:
demo:
redis:
host: 172.168.1.198
port: 6379
timeout: 3000
password:
max-total: 10
max-wait-millis: 10000
max-idle: 10
编写一个单元测试类进行测试,代码如下:
package com.example.firstproject.starter;
import com.example.starter.demo.component.RedisComponent;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class StarterTests {
@Autowired
private RedisComponent redisComponent;
@Test
public void redisTest() {
String key = "redisTest";
String value = "success!!!!!";
boolean success = redisComponent.set(key, value, 3600);
log.info("set value to redis {}!", success ? "success" : "failed");
String result = redisComponent.get(key, String.class);
log.info("get value from redis: [{}]", result);
}
}
自定义 Spring Boot Starter的更多相关文章
- 年轻人的第一个自定义 Spring Boot Starter!
陆陆续续,零零散散,栈长已经写了几十篇 Spring Boot 系列文章了,其中有介绍到 Spring Boot Starters 启动器,使用的.介绍的都是第三方的 Starters ,那如何开发一 ...
- 最详细的自定义Spring Boot Starter开发教程
1. 前言 随着Spring的日渐臃肿,为了简化配置.开箱即用.快速集成,Spring Boot 横空出世. 目前已经成为 Java 目前最火热的框架了.平常我们用Spring Boot开发web应用 ...
- 自定义spring boot starter 初尝试
自定义简单spring boot starter 步骤 从几篇博客中了解了如何自定义starter,大概分为以下几个步骤: 1 引入相关依赖: 2 生成属性配置类: 3 生成核心服务类: 4 生成自动 ...
- Spring Boot(3)---自定义spring boot starter 问题
1. "Failed to process import candidates for configuration class [com.simple.....]": 主要原因: ...
- Sping Boot入门到实战之实战篇(一):实现自定义Spring Boot Starter——阿里云消息队列服务Starter
在 Sping Boot入门到实战之入门篇(四):Spring Boot自动化配置 这篇中,我们知道Spring Boot自动化配置的实现,主要由如下几部分完成: @EnableAutoConfigu ...
- Spring Boot Starter 开发指南
Spring Boot Starter是什么? 依赖管理是任何复杂项目的关键部分.以手动的方式来实现依赖管理不太现实,你得花更多时间,同时你在项目的其他重要方面能付出的时间就会变得越少. Spring ...
- Spring Boot (一): Spring Boot starter自定义
前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...
- 自定义的Spring Boot starter如何设置自动配置注解
本文首发于个人网站: 在Spring Boot实战之定制自己的starter一文最后提到,触发Spring Boot的配置过程有两种方法: spring.factories:由Spring Boot触 ...
- Spring Boot Starter自定义实现三步曲
实现自定义的spring boot starter,只需要三步: 1.一个Bean 2.一个自动配置类 3.一个META-INF/spring.factories配置文件 下面用代码演示这三步. 项目 ...
随机推荐
- 发送post请求
题目: http://123.206.87.240:8002/post/ Brup抓包 1.修改Get 为 POST 2.添加 Content-Type: application/x-www-form ...
- leetcode刷题-66加一
题目 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: ...
- LiteOS间歇计算技术:IOT终端真正感受“电量自由”
摘要:LiteOS间歇计算为什么能使物联网终端实现长续航? 物联网设备.场景复杂多样,小到智能穿戴的耳机,大到大型基建设备,更有我们陌生而又熟悉场景,例如深海探测.森林监控.野生动物跟踪等等能量采集场 ...
- JS数据类型及常用操作
1.字符串 2.数字类型 3.布尔类型 4.数组类型 5.字典
- 安装Linux的CentOS操作系统 - 初学者系列 - 学习者系列文章
Linux系统对于一些熟悉Windows操作系统的用户来说可能比较陌生,但是它也是一种多用户.多任务的操作系统,现在也发展成为了多种版本的操作系统了.如果想对该系统进行学习,请下载这个学习文档:htt ...
- 现代C++教程:高速上手(四)-容器
1.线性容器 std::array与std::vector不同的是,array对象的大小是固定的,如果容器大小是固定的,那么可以优先考虑使用std::array容器. 由于std::vector是自动 ...
- 9.Lock-锁
- 《zookeeper原理与实践》笔记
第1章 分布式架构 1.1 分布式 分布式特点:分布性.对等性.并发性.缺乏全局时钟.故障总是会发生. 分布式问题:通讯异常.网络分区(脑裂).三态.节点故障. 1.2 ACID到CAP/BASE ...
- SSTI服务器模板注入(以及关于渲染,solt的学习)&&[BJDCTF2020]The mystery of ip 1
ssti服务器模板注入 ssti:利用公共 Web 框架的服务器端模板作为攻击媒介的攻击方式,该攻击利用了嵌入模板的用户输入方式的弱点.SSTI 攻击可以用来找出 Web 应用程序的内容结构. slo ...
- ARM架构下的Docker环境,OpenJDK官方没有8版本镜像,如何完美解决?
为什么需要ARM架构下的OpenJDK8的Docker镜像? 对现有的Java应用,之前一直运行在x86处理器环境下,编译和运行都是JDK8,如今在树莓派的Docker环境运行(或者其他ARM架构电脑 ...