前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >SpringBoot自定义starters

SpringBoot自定义starters

作者头像
全栈程序员站长
发布于 2022-11-03 07:54:02
发布于 2022-11-03 07:54:02
27900
代码可运行
举报
运行总次数:0
代码可运行

SpringBoot自定义starters

1、简介

SpringBoot最强大的功能就是把我们常用的场景抽象成一个个starter(场景启动器),我们通过引入springBoot为我们提供这些场景启动器,我们再进行少量的配置就能使用相应的功能。但是,SpringBoot不能包含所有的场景,经常需要我们自定义starter,来简化我们对springBoot的使用。

2、如何自定义starter

2.1、编写自定义模块

根据SpringBoot官方的介绍,一般情况下我们需要创建两个模块,一个是autoconfigure,一个是starter,autoconfigure里面装着自定义配置类,而starter只是负责向外提供调用的接口,所以下面我们先创建了几个模块

2.1.1、一个父模块和两个子模块

SpringBoot创建父子级Maven项目

2、创建完成之后,我们需要在starter的pom文件中引入autoconfigure,又因为我们这个是以web首页为例子,所以还要在autoconfigure里面引入web的starter启动器。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<!-- 还需要检查父项目的pom文件是否有 -->
<packaging>pom</packaging>

starter的pom文件:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?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>springboot_custom_starter</artifactId>
<groupId>cool.ale</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>
自定义启动器starter
</description>
<artifactId>dujlc-spring-boot-starter</artifactId>
<dependencies>
<!-- 引入autoconfigure -->
<dependency>
<groupId>cool.ale</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>dujlc-spring-boot-autoconfigure</artifactId>
</dependency>
<!-- 如果需要引用其它的类库,可以在这里引用 -->
</dependencies>
</project>

autoconfigure的pom文件:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?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>springboot_custom_starter</artifactId>
<groupId>cool.ale</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dujlc-spring-boot-autoconfigure</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>

2.1.2、写上相应的配置类等等

一共涉及到三个Java

类名称

含义

HelloAutoConfiguration

自动配置类,可调用我们自动配置实现的具体功能

HelloProperties

自动配置属性文件,配置着我们的这个自动配置可设置哪些属性

IndexController

HelloAutoConfiguration 调用的具体实现

代码举例如下:

HelloAutoConfiguration

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.starter.ale;
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;
@Configuration
// 必须要配置这个属性才会生效
@ConditionalOnProperty("cool.ale.starter.name")
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration { 

@Autowired
private HelloProperties helloProperties;
/** * 为web应用添加一个首页 */
@Bean
public IndexController indexController(){ 

return new IndexController(helloProperties);
}
}

HelloProperties

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.starter.ale;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("cool.ale.starter")
public class HelloProperties { 

private String name;
public String getName() { 

return name;
}
public void setName(String name) { 

this.name = name;
}
}

IndexController

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.starter.ale;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController { 

HelloProperties helloProperties;
public IndexController() { 
}
public IndexController(HelloProperties helloProperties) { 

this.helloProperties = helloProperties;
}
/** * 首页 * @return */
@RequestMapping("/")
public String index(){ 

return helloProperties.getName() + "欢迎你";
}
}

在这里我们发现一个问题,就是HelloProperties类里面的注解报错,我们需要做如下操作:

我们发现勾选之后还报错,这个其实已经和编码没有关系了,只是一个提示,我们可以通过下面的方法让报错不提示:

在报错行上alt+enter键,弹出如下图所示框,依次选中即可(先忽略图中代码,这是我从另一个项目截的图),原来的代码是第二张图的@ConfigurationProperties报错:

2.1.3、定义spring.factories

定义这个文件的目的就是让SpringBoot找到我们的自定义的自动配置类,如下所示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.starter.ale.HelloAutoConfiguration

2.1.4、依次打包

代码书写完之后,我们将这三个打包,一定要注意打包顺序,starter在最后打包,依次点击三个包的install,如下图所示:

2.1.5、使用其它模块进行调用

1、在其它模块中的全局配置文件中配置下面属性:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
cool.ale.starter.name="ale"

2、启动首页访问,如下图所示:

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/203465.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月23日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Spring Boot 自定义 starter
SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能。即使是这样,springboot也不能囊括我们所有的使用场景,往往我们需要自定义starter,来简化我们对springboot的使用。
程序员果果
2019/05/15
7810
SpringBoot自定义starter
mybatis-spring-boot-starter;自定义启动器名-spring-boot-starter
程序员阿杜
2021/03/16
4320
springboot之自定义starter
3、在该工程中点击+,选择new module,新建一个Spring Initializr工程
西西嘛呦
2020/08/26
3660
springboot之自定义starter
Spring Boot - 手把手教小师妹自定义Spring Boot Starter
SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,再进行少量的配置就能使用相应的功能。
小小工匠
2021/08/17
3K0
Spring Boot - 手把手教小师妹自定义Spring Boot Starter
springboot自定义starter
第一步:创建starter工程hello-spring-boot-starter并配置pom.xml文件
无敌小菜鸟
2022/02/23
8270
springboot自定义starter
Spring Boot 自定义starter
SpringBoot 用起来方便,它默认集成了 Java 的主流框架。这也是 SpringBoot 的一大特色,使用方便,需要什么框架或者技术,只需要引入对应的 starter 即可。目前官方已经集成的各大技术的启动器,可以查看 文档。
吟风者
2019/08/26
1.1K0
第二十八章:SpringBoot使用AutoConfiguration自定义Starter
在我们学习SpringBoot时都已经了解到starter是SpringBoot的核心组成部分,SpringBoot为我们提供了尽可能完善的封装,提供了一系列的自动化配置的starter插件,我们在使用spring-boot-starter-web时只需要在pom.xml配置文件内添加依赖就可以了,我们之前传统方式则是需要添加很多相关SpringMVC配置文件。而spring-boot-starter-web为我们提供了几乎所有的默认配置,很好的降低了使用框架时的复杂度。 因此在使用xx.starter时
恒宇少年
2018/06/27
1.5K0
Springboot 系列(十五)如何编写自己的 Springboot starter
Springboot 中的自动配置确实方便,减少了我们开发上的复杂性,那么自动配置原理是什么呢?之前我也写过了一篇文章进行了分析。 Springboot 系列(三)Spring Boot 自动配置。
未读代码
2019/11/04
3.9K0
SpringBoot之SpringBoot自定义Starter场景启动器
  相信能看到这里的应该清楚什么是场景启动器,而SpringBoot能够快速整合第三方环境依靠的就是Maven整合依赖+自定义Starter
彼岸舞
2021/02/02
6220
SpringBoot之旅-启动原理及自定义starter
SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置。
烂猪皮
2023/09/04
3020
SpringBoot之旅-启动原理及自定义starter
SpringBoot自定义Starter
在前面两章 SpringBoot入门 、SpringBoot自动配置原理 的学习后,我们对如何创建一个 SpringBoot 项目、SpringBoot 的运行原理以及自动配置等都有了一定的了解。如果我们系统中也想要拥有自动配置的功能,可以自己编写一个starter (启动器),想想就觉得很酷,因为这意味着我们不仅有自己定义的自动配的功能,而且具有更通用的耦合度更低的配置。
JAVA葵花宝典
2019/05/24
3970
19-SpringBoot自动配置-自定义starter实现
在前面我们使用了不少 SpringBoot 的起步依赖,例如 mybatis 、 redis 等起步依赖。那么本篇章,我们首先参考 mybatis 的起步依赖分析,然后自己写一个 redisTemplate 的起步依赖。
Devops海洋的渔夫
2022/03/23
6480
19-SpringBoot自动配置-自定义starter实现
SpringBoot 启动配置原理
2. 先创建一个空项目,然后 oy-spring-boot-starter(用 maven 创建)和 oy-spring-boot-starter-autoconfigurer(spring Initializr 创建)
OY
2022/03/17
2790
SpringBoot 启动配置原理
Spring Boot如何自定义Starter
如果我们系统中想要拥有自动配置的功能,可以自己编写一个starter (启动器),想想就觉得很酷,因为这意味着我们不仅有自己定义的自动配的功能,而且具有更通用的耦合度更低的配置。
Bug开发工程师
2019/05/05
6540
Spring Boot如何自定义Starter
手把手教你自定义Spring Boot Starter
官方提供的Spring Boot Starter涵盖面非常广,几乎所有流行的组件和方案都可以找到对应的封装。不过每个系统总会有自己的公共代码,可以自己进行Starter的封装。
李鸿坤
2020/08/13
7520
手把手教你自定义Spring Boot Starter
Spring Boot 自定义 Starter
我们在学习springboot的过程中发现springboot的使用起来非常的简单和方便,在使用springboot明显不用像ssm框架那样配置一大推动的东西,虽说如果熟练的话用起来并不难,但是很烦。
技术从心
2019/10/13
6940
springboot-helloworld
解决办法:1、打开配置 2、把自己本地的Maven仓库settings.xml加进去后,选择apply后,ok就好了
桑鱼
2020/03/17
5350
springboot-helloworld
SpringBoot的第一个web项目
这一节主要是讲springboot搭建简单的web项目。 首先pom文件新增spring-boot-starter-web依赖,pom文件如下所示 <?xml version="1.0" encodi
dalaoyang
2018/04/28
6260
SpringBoot的第一个web项目
springBoot学习(五)springBoot自定义banner与web开发
通过 springBootTestRun.setBannerMode(Banner.Mode.OFF);
杨小杰
2019/07/04
7480
springBoot学习(五)springBoot自定义banner与web开发
玩转 Spring Boot 原理篇(自动装配前凑之自定义Stater)
玩转 Spring Boot 集成篇(Actuator、Spring Boot Admin)
一猿小讲
2022/04/12
2750
玩转 Spring Boot 原理篇(自动装配前凑之自定义Stater)
相关推荐
Spring Boot 自定义 starter
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文