基于springboot2.5.5自建启动器starter制品库
【README】
本文po出了自建springboot 启动器步骤;
【1】新建2个starter相关组件
根据 mybatis-spring-boot-starter,我们看到 自建starter需要两个组件,分别是 xxx-spring-boot-starter, xxx-spring-boot-starter-autoconfigure ;
其中,xxx-spring-boot-starter负责引入 xxx-spring-boot-starter-autoconfigure 依赖; xxx-spring-boot-starter-autoconfigure 负责定义相关配置;
step1,新建空项目 ;
项目名称为: springbt-08-starter-diy2;
step2,在空项目上新建2个组件,分别为
lisi-spring-boot-starter 和 lisi-spring-boot-starter-autoconfigure;
其中, lisi-spring-boot-starter为 单纯的maven项目;
lisi-spring-boot-starter-autoconfigure 通过 spring Initializer 工具创建;
step3, lisi-spring-boot-starter 引入 lisi-spring-boot-starter-autoconfigure 依赖;
lisi-spring-boot-starter组件的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.cmc.starter</groupId><artifactId>lisi-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version><dependencies><!--引入 lisi-spring-boot-starter-autoconfigure 依赖--><dependency><groupId>com.cmc.starter</groupId><artifactId>lisi-spring-boot-starter-autoconfigure</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>
并把测试目标目录删除, lisi-spring-boot-starter目录结构如下:
step4, lisi-spring-boot-starter-autoconfigure 组件的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.5.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.cmc.starter</groupId><artifactId>lisi-spring-boot-starter-autoconfigure</artifactId><version>0.0.1-SNAPSHOT</version><name>lisi-spring-boot-starter-autoconfigure</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></dependencies></project>
【2】开发 starter-autoconfigure 启动器配置组件
step1, 新建 HelloService,HelloProperties, HelloServiceAutoConfiguration ;
package com.cmc.starter.lisi;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "cmc.hello")
public class HelloProperties {private String prefix;private String suffix;public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;}
}
package com.cmc.starter.lisi;public class HelloService {HelloProperties helloProperties;public String sayHelloCmc(String name) {return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();}public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties = helloProperties;}
}
package com.cmc.starter.lisi;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@ConditionalOnWebApplication // web应用才生效
@EnableConfigurationProperties(HelloProperties.class) // 使属性文件生效
public class HelloServiceAutoConfiguration {@AutowiredHelloProperties helloProperties;@Beanpublic HelloService helloService() {HelloService helloService = new HelloService();helloService.setHelloProperties(helloProperties);return helloService;}
}
step2,在资源类路径下新建 META-INF/spring.factories 属性文件;添加 自动配置类全限定类名;
# spring.factories# Auto Config
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.cmc.starter.lisi.HelloServiceAutoConfiguration
参考的是 spring-boot-autoconfigure.jar 中的 META-INF/spring.factories的内容;
目录结构如下:
step3,先把 lisi-spring-boot-starter-autoconfigure 制品库安装到本地仓库
step4,再把 lisi-spring-boot-starter 制品库安装到本地仓库;
【3】自建starter使用
step1, 新建项目,只需要引入 lisi-spring-boot-starter依赖 即可, 因为 lisi-spring-boot-starter 引入了 lisi-spring-boot-starter-autoconfigure,传递依赖;
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.5.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.cmc</groupId><artifactId>springboot-08-starter-test2</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-08-starter-test2</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!--引入自定义的starter--><dependency><groupId>com.cmc.starter</groupId><artifactId>lisi-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency><!-- 测试库 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- springboot web库 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
step2, 新建测试项目;编写 Controller,引入 HelloService
import com.cmc.starter.lisi.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@AutowiredHelloService helloService;@GetMapping("/hello")public String hello() {return helloService.sayHelloCmc("zhangsan");}
}
step3,HelloServiceAutoConfiguration有两个属性前缀后缀,需要配置;在 application.properties 中配置如下:
cmc.hello.prefix=chengdu
cmc.hello.suffix=028
step4,启动springboot项目,并访问
localhost:8080/hello ;
基于springboot2.5.5自建启动器starter制品库相关推荐
- Spring Boot的启动器Starter详解
Spring Boot的启动器Starter详解 作者:chszs,未经博主允许不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs Spring Boot ...
- Spring Boot(3)---Spring Boot启动器Starter详解
Spring Boot的启动器Starter详解 Spring Boot 简化了 Spring 应用开发,不需要配置就能运行 Spring 应用, Spring Boot 管理 Spring 容器.第 ...
- agilebpm脑图_干货基于SpringBoot2开发的Activiti引擎流程管理项目脚手架
干货基于SpringBoot2开发的Activiti引擎流程管理项目脚手架 [干货]基于SpringBoot2开发的Activiti引擎流程管理项目脚手架 前言 在工作中,难免会遇到需要开发基于流程管 ...
- 系统接口502异常_基于SpringBoot2.0的后台权限管理系统
简介 基于SpringBoot2.0的后台权限管理系统界面简洁美观敏捷开发系统架构.核心技术采用Spring.MyBatis.Shiro没有任何其它重度依赖. 互联网云快速开发框架,微服务分布式代码生 ...
- 20万数据 sql 快还是 java快?_基于SpringBoot2.0开发的,轻量级的,前后分离Java开发平台...
项目说明 MintLeaf-Fast是一个基于SpringBoot2.0开发的,轻量级的,前后端分离的Java快速开发平台 开箱即用,节省开发时间,提升开发效率,能够快速开发项目并交付的接私活利器 支 ...
- 基于RGB-D数据的语义建图
点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 本文由知乎作者robot L授权转载,不得擅自二次转载.原文链接:https://zhuanlan.z ...
- 基于SpringBoot2的房屋租赁系统的设计与实现-计算机、软件工程、网络工程等专业毕设
基于SpringBoot2的房屋租赁系统的设计与实现-毕业设计 摘要 主要内容 系统整体设计 系统功能展示 首页搜索 房屋推荐 租赁房屋 数据可视化 上传待租房源 总结 摘要 好吧,毕业半年了,随着工 ...
- SpringBoot 自定义实现一个启动器starter 教程。
说明:springboot 官方给我们提供了很多启动器如:elasticsearch,aop,redis...等等 但是实际开发中,可能不同公司的业务不同需要定制化一个通用的专属的启动器来满足公司内部 ...
- 视频教程-基于springboot2.x+layui+shiro+redis整合前后端分离的权限管理系统-Java
基于springboot2.x+layui+shiro+redis整合前后端分离的权限管理系统 拥有八年的Java项目开发经验,擅长Java.vue.SpringBoot.springCloud.sp ...
最新文章
- 一种互补间歇振荡器工作电压
- 无根树转为有根数(图论) By ACReaper
- VHDL四选一数据选择器和基本触发器的设计
- 前端学习(1132):正则表达式学习目标
- SIP协议(基础技术知识)
- 基于Alluxio系统的Spark DataFrame高效存储管理技术
- .NET 指南:安全编码概览
- 如何连接到远程SQL Server
- 【2012百度之星资格赛】F:百科蝌蚪团
- VS中编辑器显示行号
- 【游戏】基于matlab GUI音乐闹钟设计【含Matlab源码 1105期】
- Java class反编译工具
- Geotools解析shp文件
- DOS的net命令详解
- win10开机的微软服务器,win10系统开机登录微软账户的操作方法
- 计算机黑屏死机,电脑死机后开机黑屏怎么办
- 用HTML5写ZZULI官网(八)
- 软件测试常考面试题-软件测试面试宝典
- 知乎上这个程序员火了,竟是因为给老板修了一 次U盘...
- “木桶原理”到底源自哪里?
热门文章
- P5641 【CSGRound2】开拓者的卓识(多项式)
- Ozon Tech Challenge 2020 (Div.1 + Div.2) F. Kuroni and the Punishment 随机化
- K - Let the Flames Begin
- 牛客题霸 [最大数] C++题解/答案
- [学习笔记] 乱世之神杀疯了 —— K-D tree
- [学习笔记] 二分图基础定理的相关证明
- P4780-Phi的反函数【dfs】
- P4045-[JSOI2009]密码【AC自动机,状压dp】
- 【jzoj】2018.2.7NOIP普及组——某【BC】组模拟赛
- ssl1761-城市问题【图论,最短路,Dijkstra】