Spring Boot 2: spring.factories
About
Where Is It Located?
src/main/resources/META-INF/spring.factoriesFormat of the File
# Format:
<interface or annotation>=<implementation class 1>,<implementation class 2>,...
# Example for auto-configuration:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.config.MyAutoConfiguration
Common Keys Used in spring.factories
spring.factoriesKey
Purpose
When to Use spring.factories
spring.factoriesExamples with Spring Boot 2
1. org.springframework.boot.autoconfigure.EnableAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfigurationUse Case
Code Example
2. org.springframework.context.ApplicationListener
org.springframework.context.ApplicationListenerUse Case
Code Example
3. org.springframework.context.ApplicationContextInitializer
org.springframework.context.ApplicationContextInitializerUse Case
Code Example
4. org.springframework.boot.env.EnvironmentPostProcessor
org.springframework.boot.env.EnvironmentPostProcessorUse Case
Code Example
5. org.springframework.boot.SpringApplicationRunListener
org.springframework.boot.SpringApplicationRunListenerUse Case
Code Example
Comparison
Key
When It Runs
Purpose
Common Use
Why and When to use spring.factories instead of @Component or @Configuration ?
spring.factories instead of @Component or @Configuration ?When to Use @Component / @Configuration
@Component / @ConfigurationExample:
When to Use spring.factories
spring.factoriesExample
If we use @Component
@ComponentIf we use spring.factories
spring.factoriesLast updated