自定义starter
前提: 了解springboot自动配置原理
简介: springboot启动类上的注解** @SpringBootApplication**含有自动配置关键注解 @EnableAutoConfiguration,这个注解通过反射 引入了 AutoConfigurationImportSelector类
该类中getAutoConfigurationEntry方法调用类中另一个方法getCandidateConfigurations,
getCandidateConfigurations会去扫描每个jar包的META-INF/下的配置文件
在spring2.7之前, 配置的文件路径为/META-INF/spring.factories
在spring2.7之后
-
配置的文件在
-
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
-
同时兼容/META-INF/spring.factories
在spring3.0之后
- 移除META-INF/spring.factories方式
- 只支持META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 增加自动配置
这里以springboot3.2.1为例
项目结构:
自定义Starter第一步:
使用idea创建一个空的springboot项目,命名为:xxx-spring-boot-starter(这是自定义starter的命名规范,当然也可以不遵守)
第二步:
导入pom依赖
<dependencies>
<!--项目原本含有依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--需要导入的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
第三步:
编写一个Properties类----SmsProperties
package com.example;
import org.springframework.boot.context.properties.ConfigurationProperties;
//这里指定前缀,与applocation.properties文件绑定
@ConfigurationProperties(prefix = "sms")
public class SmsProperties {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
第四步:
编写咱们实际的业务类,也就是想要实现的功能------SmsService
package com.example;
import org.springframework.stereotype.Service;
@Service //注解要加,交给IOC托管,不然会找不到SmsService类
public class SmsService {
SmsProperties smsProperties;
public SmsProperties getSmsProperties() {
return smsProperties;
}
public void setSmsProperties(SmsProperties smsProperties) {
this.smsProperties = smsProperties;
}
//业务
public String say(String content){
return "SmsProperties{" +
"username='" + smsProperties.getUsername()+ '\'' +
", password='" + smsProperties.getPassword()+ '\'' +
", content='" + content +
'}';
}
}
第五步:
编写自动配置类------SmsAutoConfiguration
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@AutoConfiguration
@ConditionalOnClass(value = SmsService.class)//当SmsService类存在时生效
@EnableConfigurationProperties(SmsProperties.class)//绑定Properties
public class SmsAutoConfiguration {
@Autowired
SmsProperties smsProperties;
@Bean
@ConditionalOnMissingBean //如果没有SmsService对象就初始化
public SmsService smsService(){
SmsService smsService = new SmsService();
smsService.setSmsProperties(smsProperties);
return smsService;
}
}
第六步:
在reesource文件夹下创建META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
将SmsAutoConfiguration类写入配置文件
com.example.SmsAutoConfiguration
第七步:
利用IDEA工具导出jar包
第八步:
导出成功后会提示路径和版本
第九步:测试
新建一个springboot项目,导入自己编写的 jar包,运行测试
编写配置文件(如果有)
测试:
测试结果:
到这里自定义一个springboot的starter就结束了
可能出现的问题:
在导入包之后,可能引用不到里面的Service对象,如图:
解决方法:
修改自定义jar包的项目pom文件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--添加以下代码-->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
参考链接:引入自定义springboot的starter后却找不到相关的类(BOOT-INF文件夹的坑)_引入一个自定义的starter,注入一个对象,报未找到该对象-CSDN博客