Spring Boot - 手把手教小师妹自定义Spring Boot Starter
文章目录
- Pre
- 自定义starter的套路
- 步骤
- 命名规范
- 官方命名空间
- 自定义命名空间
- 实战
- 创建一个父maven项目:springboot_custome_starter
- 创建 两个Module: artisan-spring-boot-starter 和 artisan-spring-boot-starter-autoconfigurer
- artisan-spring-boot-starter (空的jar文件,仅仅提供辅助性依赖管理)
- pom
- artisan-spring-boot-starter-autoconfigurer
- pom
- CustomProperties
- IndexController
- CustomAutoConfiguration 自动配置类
- spring.factories
- 打包发布
- 引用自定义starter测试
Pre
SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,再进行少量的配置就能使用相应的功能。
但有些时候,springboot也不能囊括我们所有的使用场景,往往我们需要自定义starter,来简化我们对springboot的使用。
那怎么搞呢?
自定义starter的套路
参照@WebMvcAutoConfiguration , 看看们需要准备哪些东西
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {.......
}
抽取以下
@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //指定条件成立的情况下自动配置类生效
@AutoConfigureOrder //指定自动配置类的顺序
@Bean //向容器中添加组件
@ConfigurationProperties //结合相关xxxProperties来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
自动配置类要能加载需要将自动配置类,配置在META-INF/spring.factories中
我们参考下 spring-boot-starter
点击maven进去
有个 spring-boot-autoconfigure的依赖
步骤
所以总结下
- 启动器(starter)是一个空的jar文件,仅仅提供辅助性依赖管理,这些依赖可能用于自动装配或其他类库。
- 需要专门写一个类似spring-boot-autoconfigure的配置模块
- 用的时候只需要引入启动器starter,就可以使用自动配置了
命名规范
官方命名空间
前缀:spring-boot-starter-
模式:spring-boot-starter-模块名
举例:spring-boot-starter-web、spring-boot-starter-jdbc
自定义命名空间
后缀:-spring-boot-starter
模式:模块-spring-boot-starter
举例:mybatis-spring-boot-starter
实战
创建一个父maven项目:springboot_custome_starter
新建一个maven工程, 父工程,pom类型 , src 目录 和 IDEA自动创建的.md文件,删除掉
pom 如下
<?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.3.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><packaging>pom</packaging><groupId>com.artisan.customstarter</groupId><artifactId>starter</artifactId><version>0.0.1-SNAPSHOT</version><name>starter</name><description>SpringBoot自定义starter</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies><build><plugins><!--提供source--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><configuration><attach>true</attach></configuration><executions><execution><phase>compile</phase><goals><goal>jar</goal></goals></execution></executions></plugin></plugins></build></project>
创建 两个Module: artisan-spring-boot-starter 和 artisan-spring-boot-starter-autoconfigurer
同样的方式创建
artisan-spring-boot-starter (空的jar文件,仅仅提供辅助性依赖管理)
pom
<?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>starter</artifactId><groupId>com.artisan.customstarter</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><description>启动器(starter)是一个空的jar文件,仅仅提供辅助性依赖管理,这些依赖需要自动装配或其他类库。</description><artifactId>artisan-spring-boot-starter</artifactId><dependencies><!--引入autoconfigure--><dependency><groupId>com.artisan.customstarter</groupId><artifactId>artisan-spring-boot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!--如果当前starter 还需要其他的类库就在这里引用--></dependencies></project>
如果使用spring Initializr创建的需要删除 启动类、resources下的文件,test文件。
artisan-spring-boot-starter-autoconfigurer
pom
<?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>starter</artifactId><groupId>com.artisan.customstarter</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>artisan-spring-boot-starter-autoconfigurer</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--‐导入配置文件处理器,配置文件进行绑定就会有提示--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency></dependencies></project>
关于自动提示的jar ,引入spring-boot-configuration-processor之后, 编译之后就会在META-INF文件夹下面生成一个spring-configuration-metadata.json的文件。
内容如下
【工程结构】
CustomProperties
package com.artisan;import org.springframework.boot.context.properties.ConfigurationProperties;/*** @author 小工匠* @version 1.0* @description: 属性配置文件* @date 2021/5/22 18:56* @mark: show me the code , change the world*/@ConfigurationProperties("artisan.custom")
public class CustomProperties {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}
IndexController
package com.artisan;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2021/5/22 18:58* @mark: show me the code , change the world*/@RestController
public class IndexController {private CustomProperties customProperties;public IndexController(CustomProperties customProperties) {this.customProperties = customProperties;}@RequestMapping("/")public String index() {return customProperties.getName();}}
CustomAutoConfiguration 自动配置类
package com.artisan;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author 小工匠* @version 1.0* @description: 模拟给web应用自动添加一个首页* @date 2021/5/22 18:55* @mark: show me the code , change the world*/@Configuration
@ConditionalOnProperty(value = "artisan.custom.name")
@EnableConfigurationProperties(CustomProperties.class)
public class CustomAutoConfiguration {@AutowiredCustomProperties cus;@Beanpublic IndexController indexController() {return new IndexController(cus);}}
spring.factories
在 resources 下创建文件夹 META-INF 并在 META-INF 下创建文件 spring.factories ,内容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.artisan.CustomAutoConfiguration
打包发布
到这儿,我们的配置自定义的starter就写完了 ,我们artisan-spring-boot-starter 和 artisan-spring-boot-starter-autoconfigurer 安装成本地jar包。
引用自定义starter测试
新建个spring boot 项目 ,引用刚才的starter
访问 http://localhost:8080/
咦 ,别忘了你自定义的属性
重启后,重新访问
因为我们开启了debug=true .找找我们自动装配的类看看
Spring Boot - 手把手教小师妹自定义Spring Boot Starter相关推荐
- 手把手教你如何使用Spring Security(上):登录授权
文章目录 一.什么是 Spring Security? 官方介绍 通俗来讲 二.初始搭建 创建 启动 三.项目原理 原理 思考 四.登录认证 登录过滤器 配置过滤器链 类补充 五.登录效果 效果演示 ...
- 手把手教你写一个spring IOC容器
本文分享自华为云社区<手把手教你写一个spring IOC容器>,原文作者:技术火炬手. spring框架的基础核心和起点毫无疑问就是IOC,IOC作为spring容器提供的核心技术,成功 ...
- TensorFlow2 手把手教你实现自定义层
TensorFlow2 手把手教你实现自定义层 概述 Sequential Model & Layer 案例 数据集介绍 完整代码 概述 通过自定义网络, 我们可以自己创建网络并和现有的网络串 ...
- 手把手教你 Tableau 自定义地理编码(十九)
手把手教你 Tableau 自定义地理编码 Tableau 绘制地图时,支持的地理位置数据有限.当我们需要 Tableau 识别我们自定义的地理位置数据时,我们可以使用 Tableau 的自定义地理编 ...
- 保姆级教程,手把手教你实现一个SpringBoot的starter
引言 什么是Spring Boot Starter呢?我们直接来看看官网是怎么介绍的吧. ❝ Starters are a set of convenient dependency descripto ...
- 手把手教你定制标准 Spring Boot starter
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 写在前面 我们每次构建一个 Spring 应用程序时,我 ...
- 手把手教你整合 SpringMvc+Spring+MyBatis+Maven
注:该教程是参考孙宇老师的<SpringMvc+Spring+Mybatis+Maven整合视频教程1>整理的,花了我六个多小时,边复习视频边调代码边写教程,保证该教程每一步都能正确执行, ...
- 手把手教你手写Spring框架
手写spring准备工作: 新建一个maven工程: 架构 新建类: package com.spring;public class keweiqinApplicationContext {priva ...
- 手把手教你 springboot 自定义注解 (含代码)
来源:moon聊技术 https://mp.weixin.qq.com/s/s-B8i1wyJESqvrKp10ytQg 女朋友 : 我想要我自己的注解,你教我! moon : 诶?你怎么突然想要自己 ...
最新文章
- 都在抢论文第一作者,如何处理?
- 关于“进程”与“线程”的最通俗解析
- word公式编辑器_论文查重算公式吗 公式怎样避免查重?
- 汉字与区位码互转(转)
- Python使用matplotlib可视化模拟正弦余弦在子图显示
- UFO报表另存为Excel提示:可能没有正确安装Excel
- 绿云酒店管理系统 服务器要求,绿云宾馆管理软件-系统基础.doc
- 翻译:俄国卫星GLONASS 简介 天基全球导航卫星系统 (GNSS)
- matlab计算三角格网面积,MATLAB中plot的用法
- 北京国际学校IB考试均分稳得一匹,IB考试结果揭秘
- 爱快docker青龙面板保姆级
- Java面试题,208道
- c语言课设菜单,c语言课程设计菜单
- 显示商品信息(java web)
- OpenStack 裸金属
- Java笨狗groovy学习笔记—Regular Expressions
- 他三流大学毕业,从学渣逆袭成上市CEO
- 视频显示输出接口及发展历史
- LKDHelper使用LKDBHelper以实体类对象进行数据库的操作,例如新建一个新闻实体类,以这个类来
- kaili更新国内源,“没有数字签名”错误
热门文章
- Linux文件被自动加属性保护,Linux下如何对文件进行权限保护以防止文件被人改动...
- SOCKET/串口通信粘包问题处理,附带详细代码
- 博易大师 行情服务器文件,博易大师目录
- C++用顶层函数重载操作符(一)
- C++字符串详解(三) 字符串的查找
- 文巾解题 01.05. 一次编辑
- Python可视化应用实战-如何制作酷炫的图表?
- 从无到有算法养成篇-算法基础常识
- 当代的设计潮流是什么_12月,潮流咖的出行攻略!
- 如何打开CMD命令行