Is there an analogue of ServiceLoader in Spring and how to use it?

∥☆過路亽.° 提交于 2019-12-24 01:16:01

问题


I am trying to find out if there is a Spring analogue of the ServiceLoader class which is part of the standard SDK's API. If there is such a class how is it used?

Please advise!


回答1:


Assume SpringFactoriesLoader is for you. From its JavaDocs:

/*
 * General purpose factory loading mechanism for internal use within the framework.
 *
 * <p>The {@code SpringFactoriesLoader} loads and instantiates factories of a given type
 * from "META-INF/spring.factories" files. The file should be in {@link Properties} format,
 * where the key is the fully qualified interface or abstract class name, and the value
 * is a comma-separated list of implementation class names. For instance:
 *
 * <pre class="code">example.MyService=example.MyServiceImpl1,example.MyServiceImpl2</pre>
 *
 * where {@code MyService} is the name of the interface, and {@code MyServiceImpl1} and
 * {@code MyServiceImpl2} are the two implementations.
 */

The sample from one of our project:

META-INF/spring.factories:

org.springframework.integration.config.IntegrationConfigurationInitializer=\
org.springframework.integration.config.GlobalChannelInterceptorInitializer,\
org.springframework.integration.config.IntegrationConverterInitializer

The implementation:

public class IntegrationConfigurationBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Set<String> initializerNames = new HashSet<String>(
                SpringFactoriesLoader.loadFactoryNames(IntegrationConfigurationInitializer.class, beanFactory.getBeanClassLoader()));

        for (String initializerName : initializerNames) {
            try {
                Class<?> instanceClass = ClassUtils.forName(initializerName, beanFactory.getBeanClassLoader());
                Assert.isAssignable(IntegrationConfigurationInitializer.class, instanceClass);
                IntegrationConfigurationInitializer instance = (IntegrationConfigurationInitializer) instanceClass.newInstance();
                instance.initialize(beanFactory);
            }
            catch (Exception e) {
                throw new IllegalArgumentException("Cannot instantiate 'IntegrationConfigurationInitializer': " + initializerName, e);
            }
        }
    }

}


来源:https://stackoverflow.com/questions/24248025/is-there-an-analogue-of-serviceloader-in-spring-and-how-to-use-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!