SpringBoot如何自定义一个starter

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));
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e1V8TYo3-1677856088329)(C:\Users\DY\AppData\Roaming\marktext\images\2023-03-03-23-04-57-image.png)]

到这里,自定义一个SpringBoot starter就成功了,如果觉得对你有帮助,就给个小赞吧👍👍👍

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值