SpringBoot如何自定义一个starter

2023-06-25,,

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.doc》

下载本文的Word格式文档,以方便收藏与打印。