SpringFactory
是Spring框架内部使用的一种通用的工厂加载机制,用于加载META-INF/spring.factories
文件内的SPI扩展。
SpringFactoriesLoader
是Spring内置的加载META-INF/spring.factories
文件的工具类,有两个方法:
loadFactoryNames(Class factoryType,ClassLoader classLoader)
:加载指定工厂类的实现类名称。loadFactories(Class factoryType,ClassLoader classLoader)
:实例化指定工厂类的实现类。其中,loadFactories()
的结果会根据Ordered
接口及@Order
和@Priority
注解指定的order
值进行排序。
META-INF/spring.factories
本质是一个properties
类型的文件,存储格式为key=values
,多个value之间用英文‘,
’分隔。
ApplicationListener
的实现类名称:@Slf4j(topic = "springFactory")
class SpringFactoriesLoaderTest {
@Test
void loadApplicationListenerFactoryNames(){
List<String> factoryNames = SpringFactoriesLoader.loadFactoryNames(ApplicationListener.class,null);
factoryNames.forEach(it -> logger.info("{}",it));
}
}
运行输出:
10:55:26.316 [main] INFO springFactory - org.springframework.boot.ClearCachesApplicationListener
10:55:26.319 [main] INFO springFactory - org.springframework.boot.builder.ParentContextCloserApplicationListener
10:55:26.319 [main] INFO springFactory - org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor
10:55:26.319 [main] INFO springFactory - org.springframework.boot.context.FileEncodingApplicationListener
10:55:26.319 [main] INFO springFactory - org.springframework.boot.context.config.AnsiOutputApplicationListener
10:55:26.319 [main] INFO springFactory - org.springframework.boot.context.config.ConfigFileApplicationListener
10:55:26.319 [main] INFO springFactory - org.springframework.boot.context.config.DelegatingApplicationListener
10:55:26.319 [main] INFO springFactory - org.springframework.boot.context.logging.ClasspathLoggingApplicationListener
10:55:26.319 [main] INFO springFactory - org.springframework.boot.context.logging.LoggingApplicationListener
10:55:26.319 [main] INFO springFactory - org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
10:55:26.320 [main] INFO springFactory - org.springframework.boot.autoconfigure.BackgroundPreinitializer
Process finished with exit code 0
ApplicationListener
的实现类:@Slf4j(topic = "springFactory")
class SpringFactoriesLoaderTest {
@Test
void loadApplicationListenerFactories(){
List<ApplicationListener> listeners = SpringFactoriesLoader.loadFactories(ApplicationListener.class, null);
listeners.forEach(it -> logger.info("{}",it.getClass()));
}
}
运行输出:
11:00:47.488 [main] INFO springFactory - class org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor
11:00:47.491 [main] INFO springFactory - class org.springframework.boot.context.config.ConfigFileApplicationListener
11:00:47.491 [main] INFO springFactory - class org.springframework.boot.context.config.AnsiOutputApplicationListener
11:00:47.491 [main] INFO springFactory - class org.springframework.boot.context.logging.LoggingApplicationListener
11:00:47.491 [main] INFO springFactory - class org.springframework.boot.context.logging.ClasspathLoggingApplicationListener
11:00:47.491 [main] INFO springFactory - class org.springframework.boot.autoconfigure.BackgroundPreinitializer
11:00:47.491 [main] INFO springFactory - class org.springframework.boot.context.config.DelegatingApplicationListener
11:00:47.491 [main] INFO springFactory - class org.springframework.boot.builder.ParentContextCloserApplicationListener
11:00:47.491 [main] INFO springFactory - class org.springframework.boot.ClearCachesApplicationListener
11:00:47.491 [main] INFO springFactory - class org.springframework.boot.context.FileEncodingApplicationListener
11:00:47.491 [main] INFO springFactory - class org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
Process finished with exit code 0