Spring Boot起步依赖:定制starter
在定制我们自己的起步依赖——xxx.spring.boot.starter之前,我们先了解下什么是Spring Boot起步依赖。
起步依赖,是Spring Boot带给我们的一项重要的便利。要理解Spring Boot起步依赖带来的好处,先让我们假设它不存在。如果没用Spring Boot的话,你会向项目里添加哪些依赖呢?要用Spring MVC的话,你需要哪个Spring依赖?你还记得Thymeleaf的Group和Artifact ID吗?你应该用哪个版本的Spring Data JPA呢?它们放在一起兼容吗?
总之,如果没有Spring Boot起步依赖的话,你就需要自己管理项目依赖关系。Spring Boot通过提供众多起步依赖降低项目依赖的复杂度。起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。
创建自己的起步依赖Starter
一个完整的Spring Boot 起步依赖一般包括两部分:
- autoconfigure 模块,包含着自动配置的代码
- starter 模块,包含 autoconfigure 模块的依赖以及其它有用的依赖。
命名规则
为起步依赖提供一个恰当的命名空间是一个好的习惯。通常我们的习惯是xxx-spring-boot-autoconfigure,假设要为了jedis创建一个起步依赖,那么一个比较好的命名方式是jedis-spring-boot-autoconfigure和jedis-spring-boot-starter。
autoconfigure模块
起步依赖的大部分内容都在autoconfigure模块,通常包括:
一个被 @ConfigurationProperties 注解的属性定义类。如:
@Component
@ConfigurationProperties(prefix="spring.jedis")
public class JedisProperties {
private String host;
private int port;
private int maxTotal=10;
private int maxIdle=5;
private int minIdle=5;
private boolean testOnBorrow=false;
private boolean testOnReturn=false;
private int timeout;
// getter and setter
...
}
一个用于实现自动配置的AutoConfigure类。例如:
@Configuration
@ConditionalOnClass({ JedisPool.class, Jedis.class, JedisPoolConfig.class})
@EnableConfigurationProperties({JedisProperties.class})
@AutoConfigureAfter({JedisProperties.class})
public class JedisAutoConfigure {
@Autowired
private JedisProperties jedisProperties;
@PostConstruct
public void checkConfigFileExists()
{
}
@Bean
@ConfigurationProperties(prefix = "spring.jedis")
public JedisPoolConfig jedisPoolConfig(){
return new JedisPoolConfig();
}
@Bean
@ConditionalOnMissingBean
public JedisPool jedisPool(JedisPoolConfig jedisPoolConfig){
return new JedisPool(jedisPoolConfig,
jedisProperties.getHost(),
jedisProperties.getPort(),
jedisProperties.getTimeout());
}
}
一个指定AutoConfigure类路径的文件 META-INF/spring.factories :
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
fun.example.spring.boot.autoconfigure.JedisAutoConfigure
这个三个文件差不多就是autoconfigure模块的全部了。
除此之外,还可以配置META-INF/spring-autoconfigure-metadata.properties元数据文件,这个文件可以提高应用的启动速度。官方原文如下:
Spring Boot uses an annotation processor to collect the conditions on auto-configurations in a metadata file (META-INF/spring-autoconfigure-metadata.properties). If that file is present, it is used to eagerly filter auto-configurations that do not match, which will improve startup time. It is recommended to add the following dependency in a module that contains auto-configurations:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
starter 模块
starter 模块完完全全是空的,唯一的目的就是提供必需的依赖。它也许长成这样:
<?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">
<parent>
<artifactId>jedis-spring-boot</artifactId>
<groupId>fun.example.spring.boot</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jedis-spring-boot-autoconfigure</artifactId>
<name>jedis-spring-boot-autoconfigure</name>
<properties>
<jedis.version>2.8.2</jedis.version>
</properties>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
</dependencies>
</project>
整个项目的配置模板可以看这里:spring-boot-demo。
Spring Boot起步依赖:定制starter的更多相关文章
- 1.spring boot起步之Hello World【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...
- (1)spring boot起步之Hello World【从零开始学Spring Boot】
Spring Boot交流平台 1.1 介绍 自从structs2出现上次的漏洞以后,对spring的关注度开始越来越浓. 以前spring开发需要配置一大堆的xml,后台spring加入了annot ...
- Spring Boot实战之定制URL匹配规则
本文首发于个人网站:Spring Boot实战之定制URL匹配规则 构建web应用程序时,并不是所有的URL请求都遵循默认的规则.有时,我们希望RESTful URL匹配的时候包含定界符". ...
- Spring Boot实战之定制type Formatters
本文首发于个人网站:Spring Boot实战之定制type Formatters 前面我们有篇文章介绍了PropertyEditors,是用来将文本类型转换成指定的Java类型,不过,考虑到Prop ...
- Spring Boot实战之定制自己的starter
本文首发于个人网站,原文地址:http://www.javaadu.online/?p=535,如需转载,请注明出处 在学习Spring Boot的过程中,接触最多的就是starter.可以认为sta ...
- Spring Boot中如何自定义starter?
Spring Boot starter 我们知道Spring Boot大大简化了项目初始搭建以及开发过程,而这些都是通过Spring Boot提供的starter来完成的.品达通用权限系统就是基于Sp ...
- spring boot 四大组件之Starter
1.概述 依赖管理是任何复杂项目的关键方面.手动完成这些操作并不理想; 你花在它上面的时间越多,你在项目的其他重要方面所花费的时间就越少. 构建Spring Boot启动器是为了解决这个问题.Star ...
- Spring Boot必备技能之Starter自定义
本文摘自于<Spring Cloud微服务 入门 实战与进阶>一书. 作者:尹吉欢 Spring Boot的方便体现在简化了很多繁琐的配置,对开发人员来说是一个福音,通过引入各种Spri ...
- Spring Boot 起步
……………………………………………………………………………………………………………… [应用配置]application.yml [port][context-path][datasource][jp ...
随机推荐
- 【嵌入式硬件Esp32】Ubuntu 1804下ESP32交叉编译环境搭建
一.ESP32概述EPS32是乐鑫最新推出的集成2.4GWi-Fi和蓝牙双模的单芯片方案,采用台积电(TSMC)超低功耗的40nm工艺,拥有最佳的功耗性能.射频性能.稳定性.通用性和可靠性,适用于多种 ...
- 【C# 开发技巧】 c#窗体关于调试界面和运行界面不一样的原因之一
如下图所示: 原因是因为主窗体属性AutoScaleMode设置为:Font了: 按自己需求将AutoScaleMode设置修改即可. 另外一个可能是系统显示-缩放与布局-要调整到100% 如下图:
- zookeeper的java api操作
zookeeper的java api操作 创建会话: Zookeeper(String connectString,int sessionTimeout,Watcher watcher) Zookee ...
- C++ 三大特性 继承(转载)
继承 继承:类的继承,就是新的类从已有类那里得到已有的特性.原有的类称为基类或父类,产生的新类称为派生类或子类. 基本语法 派生类的声明: class 派生类名:继承方式 基类名1, 继承方式 基类名 ...
- Google BERT
概述 BERT的全称是Bidirectional Encoder Representation from Transformers,即双向Transformer的Encoder,因为decoder是不 ...
- [SQL] - 报表查询效率优化
背景 系统将数据对象JSON序列化后存放到数据库字段中.Report 模块需要获取实时数据对象数值,当前在SQL中进行数值判断的耗时长,效率低. 分析 当前执行效率低主要是程序结构设计的不合理. SQ ...
- python学习-59 hashlib模块
hashlib模块 用于加密相关的操作,3.x里代替了md5模块和sha模块 加密功能 import hashlib obj = hashlib.md5() # 如果在md5里加上自己设置的参数,别的 ...
- windows下使用 fdfs_client 上传文件(转)
https://www.cnblogs.com/kindleheart/p/10134502.html 注意:client = Fdfs_client(r"绝对路径"),一定要是绝 ...
- Linux入职基础-1.2_U盘安装RedHat5具体步骤
从U盘安装RedHat5具体步骤 从U盘安装RedHat Linux的具体步骤: 准备工作: RHEL_5.6_i386_DVD.ISO文件 具体步骤: 1.解压并用ultraiso软件打开rhel- ...
- Jquery DataTables 服务器后端分页 Ajax请求添加自定义参数.
项目使用AdminLTE(基于Bootstrap 二次开发的框架)作为开发框架. 使用DataTables 的时候部分页面需要传参 给后台做筛选过滤. 但是不知道怎么将DataTables的参数 和自 ...