SpringBoot spring.factories 文件自动装配项

January 03, 2023 作者: yijianhao 分类: SpringBoot 浏览: 470 评论: 0

写中间件的时候常会用到spring.factories文件,但是可能由于自动装配的配置项太多,所以新版的SpringBoot已经将自动装配的配置项独立了出来。

在springboot项目中可用的配置项:

参考:https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

自动装配

在springboot 3(2.7.x开始被标为过期)之后不能在spring.factories文件中定义自动装配项org.springframework.boot.autoconfigure.EnableAutoConfiguration,需要新建META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,再将需要进行自动装配的配置写入这个文件,每行一个配置。

springboot 3之前在META-INF/spring.factories中:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.test.YourAutoConfigurationA,\
 com.example.test.YourAutoConfigurationB

springboot 3在META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中:

com.example.test.YourAutoConfigurationA
com.example.test.YourAutoConfigurationB

注意,并不是spring.factories文件过期,而是spring.factories中的自动装配配置项过期

参考

  1. https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes#auto-configuration-registration
  2. https://github.com/spring-projects/spring-boot/issues/29699
  3. https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories
  4. https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
  5. https://blog.pchudzik.com/201903/spring-factories/
#配置(2)

评论