基于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 ;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/329909.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java命令学习系列(二)——Jstack

转载自 Java命令学习系列&#xff08;二&#xff09;——Jstackjstack是java虚拟机自带的一种堆栈跟踪工具。功能 jstack用于生成java虚拟机当前时刻的线程快照。线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合&#xff0c;生成线程快照的主要目的是定位线程出…

python中ls是什么_使用Python代码实现Linux中的ls遍历目录命令的实例代码

一、写在前面 前几天在微信上看到这样一篇文章&#xff0c;链接为&#xff1a;https://www.jb51.net/it/692145.html&#xff0c;在这篇文章中&#xff0c;有这样一段话&#xff0c;吸引了我的注意&#xff1a;在 Linux 中 ls 是一个使用频率非常高的命令了&#xff0c;可选的参…

spring中stereotype注解Component,Repository,Service,Controller

【README】 本文介绍了 spring4.0 下 org.springframework.stereotype 的注解类型&#xff0c;俗称刻板型注解&#xff08;一成不变型&#xff09;&#xff1b; 包括 Component&#xff0c; Repository&#xff0c;Service&#xff0c; Controller &#xff1b; 目录 【REA…

[中级]Java命令学习系列(五)——jhat

转载自 [中级]Java命令学习系列&#xff08;五&#xff09;——jhatjhat(Java Heap Analysis Tool),是一个用来分析java的堆情况的命令。之前的文章讲到过&#xff0c;使用jmap可以生成Java堆的Dump文件。生成dump文件之后就可以用jhat命令&#xff0c;将dump文件转成html的形式…

转:IDEA 创建类注释模板和方法注释模板

转自&#xff1a; IDEA 创建类注释模板和方法注释模板 - 简书  在使用Idea的时候&#xff0c;它的注释模板很简单&#xff0c;不够详细&#xff1b;所有大多数开发者都想设置一个比较详细的注释模板&#xff0c;我现在把我了解的创建类注释模板和方法注释模板的操作记录下来…

mappedbytebuffer_Java NIO Buffer【MappedByteBuffer】概述与FileChannel的联系

“ NIO【Non-blocking IO非阻塞式IO】&#xff0c;可以让你非阻塞的使用IO&#xff0c;例如&#xff1a;当线程从通道读取数据到缓冲区时&#xff0c;线程还是可以进行其他事情。当数据被写入到缓冲区时&#xff0c;线程可以继续处理它。从缓冲区写入通道也类似&#xff0c;主要…

[初级]Java命令学习系列(六)——jinfo

转载自 [初级]Java命令学习系列&#xff08;六&#xff09;——jinfojinfo可以输出java进程、core文件或远程debug服务器的配置信息。这些配置信息包括JAVA系统参数及命令行参数,如果进程运行在64位虚拟机上&#xff0c;需要指明-J-d64参数&#xff0c;如&#xff1a;jinfo -J-…

idea 调整代码格式

1. 格式化代码时&#xff0c; 不想格式化注释&#xff0c; refer2 IDEA格式化代码时&#xff0c;不想格式化注释怎么办&#xff1f;_缘自天方的博客-CSDN博客_idea不格式化注释很简单&#xff0c;只需要把 Enable JavaDoc formatting 去掉选中状态即可。附图如下&#xff1a;h…

python递归算法_python递归算法(上)

什么是递归 在函数内部&#xff0c;是可以调用其他函数的。如果一个函数在内部调用自身&#xff0c;就称这个函数就是递归函数。举个例子&#xff1a; 实现一个可以自定义重复打印你好的函数。 要实现重复打印&#xff0c;可能我们立马就会想到使用循环。如果要求不能使用循环呢…

[初级]Java命令学习系列(七)——javap

转载自 [初级]Java命令学习系列&#xff08;七&#xff09;——javapjavap是jdk自带的一个工具&#xff0c;可以对代码反编译&#xff0c;也可以查看java编译器生成的字节码。一般情况下&#xff0c;很少有人使用javap对class文件进行反编译&#xff0c;因为有很多成熟的反编译…

局域网物理机怎么访问虚拟机

前言 友链: 原文地址 ❓-背景- 现有物理机A&#xff0c;B。A中安装了虚拟机VM上面部署了服务&#xff08;这里以mysql为例&#xff09;。B需要访问A虚拟机中部署的服务。 -环境- 物理机A&#xff08;192.168.135.161&#xff09;物理机B&#xff08;192.168.135.162&#xf…

springmvc新建拦截器

【1】web.xml中配置springmvc的配置文件路径 <!-- 配置 DispatcherServlet --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!…

householder变换qr分解matlab_【基础教程】Matlab实现傅里叶变换

傅立叶变换傅立叶变换是一种常见的分析方法&#xff0c;傅立叶变换将满足一定条件的函数表示为一些函数的加权和(或者积分)。可以分为四个类别&#xff1a; 1. 非周期连续性信号 对应于傅里叶变换&#xff0c;频域连续非周期 2. 周期性连续性信号 对应于傅立叶级数&#xff0c;…

Java开发必会的Linux命令

转载自 Java开发必会的Linux命令 必会Linux命令清单查找文件 find / -name filename.txt 根据名称查找/目录下的filename.txt文件。 find . -name "*.xml" 递归查找所有的xml文件 find . -name "*" |xargs grep "hello" 递归查找所有文件内容中包…

http连接池

转自 &#xff1a; 最近学习了Http连接池 - 五月的仓颉 - 博客园 【1】使用线程池与否的程序性能 我的任务定义&#xff1a;从0 累加到 100w&#xff1b; public class ThreadPoolMain {/*** 线程池测试*/private static final AtomicInteger THREAD_EXECUTED_TOTAL new At…

Quartz定时任务的基本搭建

前言 个人地址&#xff1a;Quartz定时任务的基本搭建 Quartz是一个完全由Java编写的开源作业调度框架&#xff0c;为在java应用程序中进行作业调度提供了简单又强大的机制。 Quartz中分为几个核心概念&#xff1a; Job - 表示一个工作&#xff08;任务&#xff09;&#xff0…

matlab 定义一个有自变量的方程_常微分方程:(第四章) 高阶微分方程

参考《常微分方程》第三版&#xff08;王高雄&#xff09;常微分方程王高雄 第四章 高阶微分方程_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili​www.bilibili.com对于高阶微分方程&#xff0c;线性部分见4、5章&#xff0c;非线性部分见6章。4.1 线性微分方程的一般理论定义&#xff…

HttpClient api-连接池

【README】 本文 refer2 HttpClient Tutorialhttps://hc.apache.org/httpcomponents-client-4.5.x/current/tutorial/pdf/httpclient-tutorial.pdf 【2.3】http连接管理器 【2.3.1】可管理的连接与连接管理器 1&#xff09;http连接简述 HTTP 连接是复杂的、有状态的、线程…

使用SpringBoot搭建一个简单的webSocket服务

前言 个人地址&#xff1a;使用SpringBoot搭建一个简单的webSocket服务 什么是WebSocket&#xff1f; WebSocket是一个HTML5新增的协议,它的目的在浏览器和服务器之间建立一个不受限的双向实时通信的通道。比如&#xff0c;服务器可以任意时刻发送消息给浏览器。它是基于TCP&am…

cas 登录之后不跳转_图解JWT如何用于单点登录

点击上方“Java知音”&#xff0c;选择“置顶公众号”技术文章第一时间送达&#xff01;作者&#xff1a;流云诸葛http://cnblogs.com/lyzg/p/6132801.html推荐阅读(点击即可跳转阅读)1. 淘宝服务端高并发分布式架构演进之路2. IntelliJ IDEA 从入门到上瘾教程&#xff0c;2019…