Use spring.factories to load classes and @EnableAutoConfiguration annotations

Records : 388

Scenario : For Spring Boot-based applications, Java classes can be loaded using META-INF/spring.factories. When providing a framework Jar package or a feign interface Jar package for other microservices to use, the microservice that references the package does not need to manually add annotations to scan the specified package. The @EnableAutoConfiguration annotation automatically loads the classes specified by spring.factories.

Version : JDK 1.8, SpringBoot 2.6.3

1. Use spring.factories to load Java classes

1.1 Create spring.factories

In the operation directory of the code project: ../src/main/resources/.

spring.factories full path: ../src/main/resources/META-INF/spring.factories.

1.2 Introduce the classes that need to be loaded

(1) Modify the content of spring.factories

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

(2) Analysis spring.factories

Use EnableAutoConfiguration in spring.factories to introduce the class that needs to be loaded. When the Spring Boot application starts, it will automatically scan the ../META-INF/spring.factories file of each Jar, so this method can be loaded into the class. At the same time, classes introduced using EnableAutoConfiguration do not require annotations such as @Component. The \ in the file is a connector.

1.3 Package and use directly

After using META-INF/spring.factories to import the class to be loaded, the com.hub.example.feign interface can be independently packaged into a jar package, and each microservice can use the jar package.

2. Use spring.factories to load the class core process

2.1 Load @SpringBootApplication annotation

Microservices or microapplications based on the Spring Boot architecture will have a startup class, which needs to be annotated with @SpringBootApplication, which is the entry identifier of the Spring Boot application.

2.2 Load @EnableAutoConfiguration annotation

In the @SpringBootApplication annotation, the @EnableAutoConfiguration annotation is included by default. Therefore, as long as the @SpringBootApplication annotation is loaded, the @EnableAutoConfiguration annotation must be loaded.

@EnableAutoConfiguration annotation full path:

org.springframework.boot.autoconfigure.EnableAutoConfiguration

2.3 Load the classes specified in spring.factories

Use EnableAutoConfiguration in spring.factories to introduce classes to be loaded. Therefore, after loading the @EnableAutoConfiguration annotation, the class specified in spring.factories will be loaded.

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

3. The difference between using spring.factories to load classes and @Component

3.1 Conclusion

(1) Use spring.factories to load classes. In general, the loaded classes and the Java package (Package) of the current microservice do not have a common package prefix. You need to scan ../META-INF/spring.factories with the @EnableAutoConfiguration annotation.

(2) Use the @Component annotation to load the class. Generally, the loaded class and the current microservice Java package (Package) have a common package prefix, for example, they are all in the com.hub.example directory.

3.2 Using spring.factories

(1) Microservice startup class HubExampleFeignApplication package path

Startup class package path: com.hub.example.HubExampleFeignApplication.

The annotation @ComponentScan of @SpringBootApplication scans the Java classes under the com.hub.example package and its subpackages by default.

(2) Use spring.factories to load classes

The loading class prefix com.hub01.example01 of the spring.factories file is inconsistent with the microservice com.hub.example, and is scanned with the help of the EnableAutoConfiguration annotation.

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

3.3 Using @Component annotations

(1) Microservice startup class HubExampleFeignApplication package path

Startup class package path: com.hub.example.HubExampleFeignApplication.

The annotation @ComponentScan of @SpringBootApplication scans the Java classes under the com.hub.example package and its subpackages by default.

(2) Use @Component annotation

The annotation @ComponentScan of @SpringBootApplication can scan the class, so directly annotate the class.

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

4.spring-boot-autoconfigure的spring.factories

Based on the spring-boot-autoconfigure-2.6.3.jar package.

4.1spring-boot-autoconfigure-2.6.3.jar包

spring-boot-autoconfigure-2.6.3.jar is the automatic annotation package of spring-boot-2.6.3.

4.2pring.factories file

In ../META-INF/spring.factories, you can see which classes are loaded by automatic annotation.

The spring.factories loading classes are listed below.

The so-called loading class is to use this class to automatically load other specified classes that need to be automatically configured and loaded.

(1)Initializers

Loading class: org.springframework.context.ApplicationContextInitializer

Function: Load the initialization program.

(2)Application Listeners

Loading class: org.springframework.context.ApplicationListener.

Function: Load the listener.

(3)Environment Post Processors

Loading class: org.springframework.boot.env.EnvironmentPostProcessor.

Function: Environment Processor.

(4)Auto Configuration Import Listeners

Loading class: org.springframework.boot.autoconfigure.AutoConfigurationImportListener.

Function: Automatic configuration import listener.

(5)Auto Configuration Import Filters

Loading class: org.springframework.boot.autoconfigure.AutoConfigurationImportFilter.

Function: Automatically configure import filters.

(6)Auto Configure

Loading class: org.springframework.boot.autoconfigure.EnableAutoConfiguration.

Function: Automatic configuration, such as automatic annotation configuration of basic frameworks such as aop, context, Rabbit, Cache, redis, kafka, jdbc, mongo, neo4j, netty, security, thymeleaf, websocket, etc.

(7)Failure analyzers

Loading class: org.springframework.boot.diagnostics.FailureAnalyzer.

Function: Fault analyzer, such as redis, jdbc, Hikari, DataSource and other fault analysis.

(8)Template availability providers

Loading class: org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider

Function: Provide FreeMarkerTemplate, MustacheTemplate, GroovyTemplate, ThymeleafTemplate, JspTemplate and other templates.

(9)DataSource initializer detectors

Loading class: org.springframework.boot.sql.init.dependency.DatabaseInitializerDetector.

Function: The data source initializes the detector.

(10)Depends on database initialization detectors

Loading class: org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector.

Function: Database initialization detector.

Above, thanks.

March 23, 2023

Guess you like

Origin blog.csdn.net/zhangbeizhen18/article/details/129740389