SpringFactory

What

SpringFactory 是Spring框架内部使用的一种通用的工厂加载机制,用于加载META-INF/spring.factories文件内的SPI扩展。

Features

SpringFactoriesLoader是Spring内置的加载META-INF/spring.factories文件的工具类,有两个方法:

其中,loadFactories()的结果会根据Ordered接口及@Order@Priority注解指定的order值进行排序。

META-INF/spring.factories本质是一个properties类型的文件,存储格式为key=values,多个value之间用英文‘,’分隔。

Usage

@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
@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