SpringBoot starter,大家应该在平常写项目中应该非常熟悉,很多依赖都会提供集成SpringBoot的依赖,这样我们用起来就非常顺手,开箱就能用,那如何自定义一个starter呢?

SpringBoot starter

SpringBoot中的一大优势就是starter,SpringBoot也提供了很多开箱即用的starter依赖,使得我们开发变更加方便和简单,遵循约定大于配置的理念。

在平常的开发过程中,我们常常会有一些模块是可以独立于业务之外的模块,我们需要把其放到一个特定的包,再通过maven引入,再对其进行配置集成到项目中,比较麻烦。但是我们可以将其封装成一个starter,这样在其他业务中,SpringBoot会将其自动装配到IOC容器中,真香!!!。

自定义一个starter

我这里就随便集成一个简单的demo

  1. 新建一个工程比如,我这里就将其称为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>
  1. 我们就可以编写自己的代码,我这里就简单写了一个,内容也很简单,就一个加密和解密的工具,主要是为了演示怎么将这个代码让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);
}
}
  1. 编写配置类,这一步是比较重要的,因为是这一步配置bean
@Configuration
@ConditionalOnClass(value = {PasswordService.class, PasswordServiceImpl.class})
public class PasswordAutoConfigure { @Bean
PasswordService passwordService() {
return new PasswordServiceImpl();
}
}
  1. 最后在resources目录下新建META-INF目录,然后再新建一个文件spring.factories,并添加以下内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.zly.springboot.config.PasswordAutoConfigure
  1. 使用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的更多相关文章

  1. SpringBoot怎么自定义一个Starter ?

    小伙伴们曾经可能都经历过整天写着CURD的业务,都没写过一些组件相关的东西,这篇文章记录一下SpringBoot如何自定义一个Starter. 原理和理论就不用多说了,可以在网上找到很多关于该方面的资 ...

  2. SpringBoot编写自定义的starter 专题

    What’s in a name All official starters follow a similar naming pattern; spring-boot-starter-*, where ...

  3. Spring boot 自定义一个starter pom

    用过springboot的自动配置会觉得非常方便,我们完全可以自己写一个starter pom,这样不仅可以有自动配置功能,而且具有更通用的的耦合度低的配置, 新建一个starter的maven项目, ...

  4. SpringBoot自定义一个starter

    @Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效 @AutoConfigureAfter //指定自动配置类的顺序 ...

  5. 尚硅谷springboot学习36-自定义starter

    自定义一个starter要引一个依赖,即我们自己写的自动配置,在这个自动配置里写我们的自动配置类,属性类等 自动配置类开始类似这样 @Configuration //指定这个类是一个配置类 @Cond ...

  6. springboot系列十四、自定义实现starter

    一.starter的作用 当我们实现了一个组建,希望尽可能降低它的介入成本,一般的组建写好了,只要添加spring扫描路径加载spring就能发挥作用.有个更简单的方式扫描路径都不用加,直接引入jar ...

  7. springBoot 自动配置原理--自己新建一个 starter

    上篇我们说到 springboot 和 SSM 框架的区别,今天我们就看看 springboot 到底为我们做了哪些事情,让我们开发变得如此简单. springboot 中起着重要作用的是 start ...

  8. 深入springboot原理——动手封装一个starter

    从上一篇文章<深入springboot原理——一步步分析springboot启动机制(starter机制)> 我们已经知道springboot的起步依赖与自动配置的机制.spring-bo ...

  9. 【SpringBoot】编写一个自己的Starter

    一.什么是Starter? 在开发过程中我们就经常使用到各种starter,比如mybatis-spring-boot-starter,只需要进行简单的配置即可使用,就像一个插件非常方便.这也是Spr ...

  10. (springboot)自定义Starter

    要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...

随机推荐

  1. mysql知识点一

    1.mysql中造成索引失效的原因有哪些?如何分析和解决? 原因: 1.like以通配符%开头索引失效 通常用的索引数据结构是B+树,而索引是有序排列的 优化:一种是使用覆盖索引,另一种是把%放后面 ...

  2. Docker 容器与镜像

    列出所有容器ID :docker ps -aq 查看所有运行或者不运行容器:docker ps -a 停止所有的container(容器),这样才能够删除其中的images:docker stop $ ...

  3. windows系统,自动设置--shutdown命令了解

    参考:https://baike.baidu.com/item/shutdown/10469108?fr=aladdin 比如你的电脑要在24:00关机,可以选择"开始运行",输入 ...

  4. 编写简单的button配合input实现上传文件操作

    <template> <button> 导入文件 <input type="file" @change="fileChange" ...

  5. 《Unix/Linux系统编程》第八周学习笔记

    <Unix/Linux系统编程>第八周学习笔记 时钟服务函数 gettimeodfay() 获取系统时间 settimeofday() 设置系统时间 time() 以秒为单位返回当前时间 ...

  6. C# DataTable操作,转载

    DataTable 排序   DataRow[] rows = dataTable1.Select("", "ord asc");   DataTable t ...

  7. centos 开启关闭网卡(禁用网卡)

    说明我之前在工作中使用的服务器很多都是多网卡服务器,他可以使用不同的网卡连接不同的网段,但是,由于个别情况突发,有时候可能需要关闭某些网卡,禁止它们访问到网络,也就是需要关闭网卡.步骤1.查看有哪些网 ...

  8. 通过Dnsmasq自建干净的DNS服务

    不晓得为撒,用网上的一些公共DNS服务的时候,总是莫名其妙的有些网站无法解析,有时候114能解析,阿里DNS不行或者腾讯DNS不行,导致总是来回切换DNS,很是烦心. 于是就想着自己搭建一个DNS服务 ...

  9. [复现]2021DASCTF实战精英夏令营暨DASCTF July X CBCTF-PWN

    EasyHeap 想可执行的地方写入orw的shellcode,利用tcachebin的df进行劫持malloc_hook 然后调用add来触发. from pwn import * context. ...

  10. table control的最小高度

    标准的表维护和不通过向导建的table control最小显示行是2. 通过向导建立的table control最小显示行是4. 前台没有任何能看出来的配置差异. 有个隐藏的最小显示行,只有把屏幕下来 ...