第三方的开源框架普遍提供了自己的spring-boot-starter,
如 mybatis-spring-boot-starter, redis-spring-boot-starter.
自己该如何实现一个spring-boot-start呢?
创建一个xxx-spring-boot-starter项目,引入相应的依赖
创建服务Service, 创建Properties对象,用来提供Service需要的配置和开关控制(Properties不是必须)
创建 XXXAutoConfiguration类,用于生成服务bean
src/resources/META-INF中创建spring.factories文件,添加AutoConfiguration配置
1、创建mjh-spring-boot-starter的方式引入对应依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>mjh-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mjh-spring-boot-starter</name>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
2、创建Testservice
public class TestService {
private String appName;
public void setAppName(String appName) {
this.appName = appName;
}
public String getEncodedAppName(){
return Base64.getEncoder().encodeToString(appName.getBytes(Charset.forName("UTF-8")));
}
}
创建TestProperties类
@ConfigurationProperties(value = "test")
public class TestProperties {
private Boolean enable;
private HelloProperties hello;
public Boolean getEnable() {
return enable;
}
public void setEnable(Boolean enable) {
this.enable = enable;
}
public HelloProperties getHello() {
return hello;
}
public void setHello(HelloProperties hello) {
this.hello = hello;
}
public static class HelloProperties {
private String appName;
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
}
}
3、创建TestServiceAutoConfiguration自动装配类
@Configuration
@EnableConfigurationProperties(TestProperties.class)
@ConditionalOnProperty(prefix ="test",value = "enable",havingValue = "true")
public class TestServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean(TestService.class)
public TestService testService(TestProperties testProperties){
TestService testService =new TestService();
testService.setAppName(testProperties.getHello().getAppName());
return testService;
}
}
ConditionalOnMissingBean代表 当前环境下没有TestService类型的Bean时才创建Bean
@ConditionalOnProperty(prefix ="test",value = "enable",havingValue = "true") 指必须存在test.enable=true的属性才会创建 Bean
4、最后在 src/resources/META-INF目录下中创建spring.factories 文件,内容如下
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.mjhspringbootstarter.autoconfigure.TestServiceAutoConfiguration
整个项目目录
![](https://img-blog.csdnimg.cn/img_convert/b69a72daf0608a1b137ef80e6cc52862.png)
maven install生成jar包
在目标项目中 引入 starter依赖
<dependency>
<groupId>com.example</groupId>
<artifactId>mjh-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
添加mjh-spring-boot-starter属性
![](https://img-blog.csdnimg.cn/img_convert/a84bc71bcbd56ac57ef92107ca2d6e31.png)
使用
@SpringBootApplication
public class TestApplication{
public static void main(String[] args) {
ApplicationContext applicationContext=SpringApplication.run(TestApplication.class, args);
TestService testService=applicationContext.getBean(TestService.class);
System.out.println("编码后的appName:"+testService.getEncodedAppName());
}
}
执行结果:
编码后的appName:bWpoVGVzdA==