springboot2.7版本发行说明:
可以看到此时已经建议使用META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
作为定义自动装配文件位置。
springboot3.0升级指南
而在3.0版本,已经是移除了。所以我们继续通过META-INF/spring.factories文件定义将不在生效。
SpringBoot3.0定义Starter的方法
一、引入依赖,pom文件配置如下:
<!-- 自动装配依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.0.6</version>
</dependency>
<!--自动装配依赖非必需依赖,该依赖作用是在使用IDEA编写配置文件有代码提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>3.0.6</version>
</dependency>
二、写一个简单的需要自动装配的类:
/**
* @Author xiongjaixin
* @Create 2023/4/27 23:27
* @Description
* @Version V1.0
*/
public class B {
public void test(){
System.out.println("hello");
}
}
三、定义自动装配类:
/**
* @Author xiongjaixin
* @Create 2023/4/27 23:26
* @Description
* @Version V1.0
*/
@Import(B.class)
public class A {
// @Bean
// public B b(){
// return new B();
// }
}
四、创建自动装配配置文件:
在resources/META-INF/spring文件夹下创建:org.springframework.boot.autoconfigure.AutoConfiguration.imports
在文件中写要进行自动装配的文件:
com.example.starter.A
如图所示:
五、打包:
使用Maven install进行打包
六、在其他地方使用:
导入该依赖即可
七、测试:
如图所示,成功定义了一个基于springboot3.0.6的starter
八、总结:
由于META-INF/spring.factories在springboot3.0已经移除,因此,我们应采用spring官方推荐的方式:在resources/META-INF/spring文件夹下创建:org.springframework.boot.autoconfigure.AutoConfiguration.imports进行自动装配