自定义spring boot starter 初尝试
自定义简单spring boot starter 步骤
从几篇博客中了解了如何自定义starter,大概分为以下几个步骤:
1 引入相关依赖;
2 生成属性配置类;
3 生成核心服务类;
4 生成自动化配置类;
5 注册配置/META-INF/spring.factories;
6 打包发布;
下面以一个简单的demo为例,一步一步说明自定义spring boot的starter的过程。
解决问题
使用Slf4j的MDC实现一个工程中的链路追踪。通过设置open的属性代表是否打开链路追踪,从而为每一个请求生成一个traceId。当然理想是比较丰满的,实际操作如果要实现上述功能,要配合AOP/Filter/Interceptor这类工具,具体参考 文章。本文只是做个demo。
引入相关依赖
Demo比较简单,所以需要的依赖也不多,如下:
<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>
生成属性配置类
属性类一定要加@ConfigurationProperties注解,用于表示这个文件是一个属性类。
在注解@ConfigurationProperties后面可以添加prefix前缀,如果open属性要设置具体值,需要这样配置,lxl.mdc.open = true;
package com.demo.spring.starter.mdc; import org.springframework.boot.context.properties.ConfigurationProperties; /**
* 自定义starter的properties类
*
* @author lxl
* @since 2018/12/10
*/
@ConfigurationProperties(prefix = "lxl.mdc")
public class MdcDemoProperties {
private Boolean open = true; public Boolean getOpen() {
return open;
} public void setOpen(Boolean open) {
this.open = open;
} }
生成核心服务类
核心服务类的作用:根据properties中lxl.mdc.open的配置,控制是否生成sessionId,并将其put到MDC中。
package com.demo.spring.starter.mdc; import java.util.UUID; import org.slf4j.MDC; /**
* 自定义starter的核心服务类
*
* @author lxl
* @since 2018/12/10
*/
public class MdcDemoService {
private final String SESSION_ID = "SESSION_ID";
private MdcDemoProperties properties; public MdcDemoService() {
} public MdcDemoService(MdcDemoProperties properties) {
this.properties = properties;
} public String traceId() {
if (null != this.properties.getOpen() && false == this.properties.getOpen()) {
MDC.put(SESSION_ID, null);;
}else{
MDC.put(SESSION_ID, UUID.randomUUID().toString());
}
return MDC.get(SESSION_ID);
}
}
生成自动化配置类
@Configuration:标识此类为一个spring配置类
@EnableConfigurationProperties(MdcDemoProperties.class):启动配置文件,可以有多个,多个配置文件这样写:value={xxProperties1.class,xxProperteis2.class....}
@ConditionalOnClass,当classpath下发现该类的情况下进行自动配置。
更多注解参考 官方说明。
package com.demo.spring.starter.mdc; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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; /***
* 自定义strter的配置类2
*
* @author lxl
* @since 2018/12/10
*/
@Configuration
@EnableConfigurationProperties(MdcDemoProperties.class)
@ConditionalOnClass(MdcDemoService.class) public class MdcDemoServiceAutoConfiguration {
@Autowired
private MdcDemoProperties properties; @Bean
@ConditionalOnMissingBean(MdcDemoService.class)
public MdcDemoService traceId() {
return new MdcDemoService(properties);
}
}
注册配置/META-INF/spring.factories
手动在resources文件下创建META-INF/spring.factories 文件,配置如下:org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.spring.starter.mdc.MdcDemoServiceAutoConfiguration
多个类通过,分隔即可。
打包发布
本地测试 可以通过 mvn install 打包命令打包成一个jar包,本地测试的话需要将mvn的settings文件的仓库改成本地地址,不然会出现找不到包的错误。
测试
1 新建一个spring boot 工程
参考 地址。
2 pom中引入mvn依赖
<dependency>
<groupId>com.alibaba.com.custom.starter</groupId>
<artifactId>mdc-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
3 mvn依赖查看
{
"groups": [
{
"name": "lxl.mdc",
"type": "com.demo.spring.starter.mdc.MdcDemoProperties",
"sourceType": "com.demo.spring.starter.mdc.MdcDemoProperties"
}
],
"properties": [
{
"name": "lxl.mdc.open",
"type": "java.lang.Boolean",
"sourceType": "com.demo.spring.starter.mdc.MdcDemoProperties"
}
],
"hints": []
}
4 配置application.properties
lxl.mdc.open = open
5 测试demo
@GetMapping(value = "/test")
public Result test(@RequestParam(name = "id") Long id) {
logger.info("==========test log requestId in controller==============");
System.out.println("applition.properties=" + env.getProperty("lxl.mdc.open"));
System.out.println("MDC.get('SESSION_ID')=" + service.traceId());
return dataplusAuthorityTenantService.testMDCInService(id);
}
6 测试输出
applition.properties=false
MDC.get('SESSION_ID')=null
applition.properties=true
MDC.get('SESSION_ID')=31f9f18d-893e-4dd7-b323-e9587968256b
p.p1 { margin: 0; font: 18px Monaco }
p.p1 { margin: 0; font: 18px Monaco }
span.s1 { color: rgba(57, 51, 255, 1) }
自定义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(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
关于Starter Spring Boot秉承"约定大于配置"的开发方式,使得我们基于Spring Boot开发项目的效率变得十分高.相信使用过Spring Boot的小伙伴都会发 ...
- spring boot docker 初尝试
Docker服务中进程间通信通过/var/run/docker.sock实现,默认服务不提供监听端口,因此使用docker remote api 需要手动绑定端口. 在centos7.2下,可以进行这 ...
- 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触 ...
随机推荐
- JNPF移动办公解决方案
市场背景 随着办公自动化系统的普及,电子化.数据化的办公方式已进入越来越多的企业和政府单位,信息化的办公系统在企事业内部编织起一套高效.畅通的信息互联体系,极大推动了企事业单位生产力的发展.但与此同时 ...
- 逻辑运算符——JavaSE基础
逻辑运算符 运算符 说明 逻辑与 &( 与) 两个操作数为true,结果才是true,否则是false 逻辑或 |(或) 两个操作数有一个是true,结果就是true 短路与 &&am ...
- Python列表推导式,字典推导式,元组推导式
参考:https://blog.csdn.net/A_Tu_daddy/article/details/105051821 my_list = [ [[1, 2, 3], [4, 5, 6]] ] f ...
- 初步了解认识正则表达式(Regex)
如果你感到这篇文章对您有所帮助,那请您给我一个免费的赞吧QWQ! 如果想要深入理解什么是正则表达式,请购买教材<形式语言与自动机>,相信学完它之后一定会让你更加理解正则表达式! 1.你的同 ...
- 华为云发布桌面IDE-CodeArts
摘要:华为伙伴暨开发者大会2022,发布华为云桌面IDE-CodeArts. 本文分享自华为云社区<华为云发布桌面IDE-CodeArts,让连接更简单.编码更智能>,作者: Huawei ...
- DAST 黑盒漏洞扫描器 第六篇:运营篇(终)
0X01 前言 转载请标明来源:https://www.cnblogs.com/huim/ 当项目功能逐渐成熟,同时需要实现的是运营流程和指标体系建设.需要工程化的功能逐渐少了,剩下的主要工作转变成持 ...
- Linux文件查找实现
文件查找 locate:非实时查找(依赖数据库的方式) find(实时查找) locate:-- 模糊搜索(不适合经常改变的文件) locate 查询系统上预建的文件索引数据库 /var/lib/ml ...
- 用python做个计算器不是轻轻松松吗~
计算器 Kivy是一个免费的开源Python库,可以快速轻松地开发高度交互的跨平台应用程序. 这里我将使用Python中的Kivy包来构建一个计算器GUI.(https://jq.qq.com/?_w ...
- NC16649 [NOIP2005]校门外的树
NC16649 [NOIP2005]校门外的树 题目 题目描述 某校大门外长度为 \(L\) 的马路上有一排树,每两棵相邻的树之间的间隔都是 \(1\) 米.我们可以把马路看成一个数轴,马路的一端在数 ...
- Linux操作系统(2):组管理和权限管理
组管理和权限管理 Outline 1.查看文件所有者:ls -ahl 2.更改文件或目录权限命令:chmod 3.更改文件或目录所有者命令:chown 4.更改文件或目录所属组命令:chgrp 1)组 ...