使用spring.factories加载类和@EnableAutoConfiguration注解

记录:388

场景:基于Spring Boot的应用,可以使用META-INF/spring.factories方式加载Java类。解决提供框架Jar包或者feign接口Jar包给其它微服务使用时,引用该包的微服务无需手动添加注解扫描指定包。@EnableAutoConfiguration注解自动加载spring.factories指定的类。

版本:JDK 1.8,SpringBoot 2.6.3

1.使用spring.factories加载Java类

1.1创建spring.factories

在代码工程的操作目录:../src/main/resources/。

spring.factories全路径:../src/main/resources/META-INF/spring.factories。

1.2引入需要加载的类

(1)修改spring.factories内容

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.hub.example.feign.fallback.CityFeignServiceFallbackImpl,\
  com.hub.example.feign.factory.CityFeignServiceFallbackFactory

(2)解析spring.factories

在spring.factories中使用EnableAutoConfiguration引入需要加载的类,Spring Boot应用在启动时,会自动扫描每个Jar的../META-INF/spring.factories文件,因此这种方式可以加载到类。同时,使用EnableAutoConfiguration引入的类不需要@Component等注解。文件中的\是连接符。

1.3打包直接使用

使用META-INF/spring.factories引入需加载类后,可以把com.hub.example.feign接口独立打包成jar包,每个微服务都可以使用jar包了。

2.使用spring.factories加载类核心流程

2.1加载@SpringBootApplication注解

基于Spring Boot架构的微服务或者微应用都会有一个启动类,在启动类上需加@SpringBootApplication注解,此注解是Spring Boot应用的入口标识。

2.2加载@EnableAutoConfiguration注解

在@SpringBootApplication注解内,默认包含@EnableAutoConfiguration注解。因此,只要加载@SpringBootApplication注解,那么@EnableAutoConfiguration注解一定会被加载。

@EnableAutoConfiguration注解全路径:

org.springframework.boot.autoconfigure.EnableAutoConfiguration

2.3加载spring.factories中指定的类

在spring.factories中使用EnableAutoConfiguration引入需加载类。因此,加载@EnableAutoConfiguration注解后,在spring.factories指定的类会被加载。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.hub.example.feign.fallback.CityFeignServiceFallbackImpl,\
  com.hub.example.feign.factory.CityFeignServiceFallbackFactory

3.使用spring.factories加载类和@Component区别

3.1结论

(1)使用spring.factories加载类,一般情况,加载的类和当前微服务的Java包(Package)没有共同包前缀,需借助@EnableAutoConfiguration注解扫描../META-INF/spring.factories。

(2)使用@Component注解加载类,一般是加载的类和当前微服务Java包(Package)有共同的包前缀,比如都在 com.hub.example目录下。

3.2使用spring.factories

(1)微服务启动类HubExampleFeignApplication包路径

启动类包路径:com.hub.example.HubExampleFeignApplication。

注解@SpringBootApplication的注解@ComponentScan默认扫描com.hub.example包及其子包下的Java类。

(2)使用spring.factories加载类

spring.factories文件的加载类前缀com.hub01.example01和微服务com.hub.example不一致,借助EnableAutoConfiguration注解扫描。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.hub01.example01.feign.fallback.CityFeignServiceFallbackImpl,\
  com.hub01.example01.feign.factory.CityFeignServiceFallbackFactory

3.3使用@Component注解

(1)微服务启动类HubExampleFeignApplication包路径

启动类包路径:com.hub.example.HubExampleFeignApplication。

注解@SpringBootApplication的注解@ComponentScan默认扫描com.hub.example包及其子包下的Java类。

(2)使用@Component注解

注解@SpringBootApplication的注解@ComponentScan能扫描到类,因此,直接在类上加注解。

com.hub.example.feign.fallback.CityFeignServiceFallbackImpl。
com.hub.example.feign.factory.CityFeignServiceFallbackFactory。

4.spring-boot-autoconfigure的spring.factories

基于spring-boot-autoconfigure-2.6.3.jar包。

4.1spring-boot-autoconfigure-2.6.3.jar包

spring-boot-autoconfigure-2.6.3.jar是spring-boot-2.6.3的自动注解包。

4.2pring.factories文件

在../META-INF/spring.factories中可以查看自动注解加载了哪些类。

以下列出spring.factories加载类。

所谓加载类,就是使用这个类去自动加载指定的其它需要自动配置和加载的类。

(1)Initializers

加载类:org.springframework.context.ApplicationContextInitializer

功能:加载初始化程序。

(2)Application Listeners

加载类:org.springframework.context.ApplicationListener。

功能:加载监听器。

(3)Environment Post Processors

加载类:org.springframework.boot.env.EnvironmentPostProcessor。

功能:环境处理器。

(4)Auto Configuration Import Listeners

加载类:org.springframework.boot.autoconfigure.AutoConfigurationImportListener。

功能:自动配置导入监听器。

(5)Auto Configuration Import Filters

加载类:org.springframework.boot.autoconfigure.AutoConfigurationImportFilter。

功能:自动配置导入过滤器。

(6)Auto Configure

加载类:org.springframework.boot.autoconfigure.EnableAutoConfiguration。

功能:自动配置,比如aop、context、Rabbit、Cache、redis、kafka、jdbc、mongo、neo4j、netty、security、thymeleaf、websocket等基础框架的自动注解配置。

(7)Failure analyzers

加载类:org.springframework.boot.diagnostics.FailureAnalyzer。

功能:故障分析仪,比如redis、jdbc、Hikari、DataSource等故障分析。

(8)Template availability providers

加载类:org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider

功能:提供FreeMarkerTemplate、MustacheTemplate、GroovyTemplate、ThymeleafTemplate、JspTemplate等模板。

(9)DataSource initializer detectors

加载类:org.springframework.boot.sql.init.dependency.DatabaseInitializerDetector。

功能:数据源初始化检测器。

(10)Depends on database initialization detectors

加载类:org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector。

功能:数据库初始化检测器。

以上,感谢。

2023年3月23日

猜你喜欢

转载自blog.csdn.net/zhangbeizhen18/article/details/129740389