SpringBoot如何自定义一个starter
SpringBoot starter,大家应该在平常写项目中应该非常熟悉,很多依赖都会提供集成SpringBoot的依赖,这样我们用起来就非常顺手,开箱就能用,那如何自定义一个starter呢?
SpringBoot starter
SpringBoot中的一大优势就是starter,SpringBoot也提供了很多开箱即用的starter依赖,使得我们开发变更加方便和简单,遵循约定大于配置的理念。
在平常的开发过程中,我们常常会有一些模块是可以独立于业务之外的模块,我们需要把其放到一个特定的包,再通过maven引入,再对其进行配置集成到项目中,比较麻烦。但是我们可以将其封装成一个starter,这样在其他业务中,SpringBoot会将其自动装配到IOC容器中,真香!!!。
自定义一个starter
我这里就随便集成一个简单的demo
- 新建一个工程比如,我这里就将其称为learn-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>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.7.8</version>
</parent>
<groupId>cn.zly</groupId>
<artifactId>learn-spring-boot-starter</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<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-starter</artifactId>
</dependency>
</dependencies>
</project>
- 我们就可以编写自己的代码,我这里就简单写了一个,内容也很简单,就一个加密和解密的工具,主要是为了演示怎么将这个代码让SpringBoot自动装配到容器中。
public class PasswordServiceImpl implements PasswordService {
@Override
public String encode(String val) {
if (val == null) {
return null;
}
byte[] bytes = Base64.getEncoder().encode(val.getBytes());
return new String(bytes);
}
@Override
public String decode(String val) {
if (val == null) {
return null;
}
byte[] decode = Base64.getDecoder().decode(val.getBytes());
return new String(decode);
}
}
- 编写配置类,这一步是比较重要的,因为是这一步配置bean
@Configuration
@ConditionalOnClass(value = {PasswordService.class, PasswordServiceImpl.class})
public class PasswordAutoConfigure {
@Bean
PasswordService passwordService() {
return new PasswordServiceImpl();
}
}
- 最后在resources目录下新建META-INF目录,然后再新建一个文件spring.factories,并添加以下内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.zly.springboot.config.PasswordAutoConfigure
- 使用Maven将这个项目进行打包,并保存到本地仓库,也可以用IDEA来打包
mvn clean install -Dmaven.test.skip=true
使用该starter
在需要使用该项目的pom.xml添加上面的项目三坐标
<dependency>
<groupId>cn.zly</groupId>
<artifactId>learn-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
进行测试,我这里是 提前建好了一个项目,并直接在测试类这里进行测试好了,结果肯定也是成功加载到IOC容器了。
@SpringBootTest(classes = HelloWorldApplication.class)
@RunWith(SpringRunner.class)
public class StudentMapperTest {
@Autowired
private PasswordService passwordService;
@Test
public void testPassword() {
String password = passwordService.encode("zly");
System.out.println(password);
System.out.println(passwordService.decode(password));
}
}

到这里,自定义一个SpringBoot starter就成功了,如果觉得对你有帮助,就给个小赞吧
SpringBoot如何自定义一个starter的更多相关文章
- SpringBoot怎么自定义一个Starter ?
小伙伴们曾经可能都经历过整天写着CURD的业务,都没写过一些组件相关的东西,这篇文章记录一下SpringBoot如何自定义一个Starter. 原理和理论就不用多说了,可以在网上找到很多关于该方面的资 ...
- SpringBoot编写自定义的starter 专题
What’s in a name All official starters follow a similar naming pattern; spring-boot-starter-*, where ...
- Spring boot 自定义一个starter pom
用过springboot的自动配置会觉得非常方便,我们完全可以自己写一个starter pom,这样不仅可以有自动配置功能,而且具有更通用的的耦合度低的配置, 新建一个starter的maven项目, ...
- SpringBoot自定义一个starter
@Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效 @AutoConfigureAfter //指定自动配置类的顺序 ...
- 尚硅谷springboot学习36-自定义starter
自定义一个starter要引一个依赖,即我们自己写的自动配置,在这个自动配置里写我们的自动配置类,属性类等 自动配置类开始类似这样 @Configuration //指定这个类是一个配置类 @Cond ...
- springboot系列十四、自定义实现starter
一.starter的作用 当我们实现了一个组建,希望尽可能降低它的介入成本,一般的组建写好了,只要添加spring扫描路径加载spring就能发挥作用.有个更简单的方式扫描路径都不用加,直接引入jar ...
- springBoot 自动配置原理--自己新建一个 starter
上篇我们说到 springboot 和 SSM 框架的区别,今天我们就看看 springboot 到底为我们做了哪些事情,让我们开发变得如此简单. springboot 中起着重要作用的是 start ...
- 深入springboot原理——动手封装一个starter
从上一篇文章<深入springboot原理——一步步分析springboot启动机制(starter机制)> 我们已经知道springboot的起步依赖与自动配置的机制.spring-bo ...
- 【SpringBoot】编写一个自己的Starter
一.什么是Starter? 在开发过程中我们就经常使用到各种starter,比如mybatis-spring-boot-starter,只需要进行简单的配置即可使用,就像一个插件非常方便.这也是Spr ...
- (springboot)自定义Starter
要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...
随机推荐
- C# Nilakantha级数逼近PI算法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Net6读取AppSettings.json
1.创建Helper类 public class AppHelper { private static IConfiguration _config; public AppHelper(IConfig ...
- Mysql用户及其权限
一.创建用户 create user 'user_name' identified by 'password'; 二.用户授权 grant [权限名] on 数据库名.表名 to user_name ...
- Unity泛型单例模式
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Singleto ...
- WSL安装Ubuntu 22.04 (2)
1.安装系统环境 1.1.设置软件包源为国内镜像 参考:Ubuntu更换国内镜像源 - 知乎 1.2.更新系统软件包 sudo apt-get update && sudo apt-g ...
- Linux完全卸载mysql的方式
//rpm包安装方式卸载查包名:rpm -qa|grep -i mysql删除命令:rpm -e –nodeps 包名 //yum安装方式下载1.查看已安装的mysql命令:rpm -qa | gre ...
- File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent
IDEA编译报以下错误 File encoding has not been set, using platform encoding UTF-8, i.e. build is platform de ...
- 基于Quartz.Net通过反射进行任务调度
通过反射加载任务调度 需求: 因为有些任务需要进行各种定时操作,因此将 Quartz.Net 简单封装了一下使用: 希望通过上传 dll 来进行每个任务的调度,所以写了个反射调度示例: Program ...
- 文心一言,通营销之学,成一家之言,百度人工智能AI大数据模型文心一言Python3.10接入
"文心"取自<文心雕龙>一书的开篇,作者刘勰在书中引述了一个古代典故:春秋时期,鲁国有一位名叫孔文子的大夫,他在学问上非常有造诣,但是他的儿子却不学无术,孔文子非常痛心 ...
- day08-自定义转换器&处理JSON&内容协商
自定义转换器&处理JSON&内容协商 1.自定义转换器 1.1基本介绍 SpringBoot 在响应客户端请求时,将提交的数据封装成对象时,使用了内置的转换器,也就是自动帮我们封装对象 ...