一文深入剖析SpingBoot SPI

简介

SPI(Service Provider Interface)是JDK内置的一种服务提供发现机制,可以用来启用框架扩展和替换组件,主要用于框架中开发,例如Dubbo、Spring、Common-Logging,JDBC等采用采用SPI机制,针对同一接口采用不同的实现提供给不同的用户,从而提高了框架的扩展性。

Java SPI实现

Java内置的SPI通过java.util.ServiceLoader类解析classPath和jar包的META-INF/services/目录 下的以接口全限定名命名的文件,并加载该文件中指定的接口实现类,以此完成调用。

示例说明

创建动态接口

public interface VedioSPI
{
    void call();
}

实现类1

public class Mp3Vedio implements VedioSPI
{
    @Override
    public void call()
    {
        System.out.println("this is mp3 call");
    }

}

实现类2

public class Mp4Vedio implements VedioSPI
{
    @Override
    public void call()
    {
       System.out.println("this is mp4 call");
    }

}

在项目的source目录下新建META-INF/services/目录下,创建cn.fntop.spi.VedioSPI文件。

一文深入剖析SpingBoot SPI_第1张图片

相关测试

public class VedioSPITest
{
    public static void main(String[] args)
    {
        ServiceLoader serviceLoader =ServiceLoader.load(VedioSPI.class);
        
        serviceLoader.forEach(t->{
            t.call();
        });
    }
}

说明:Java实现spi是通过ServiceLoader来查找服务提供的工具类。

运行结果

this is mp4 call
this is mp3 call

源码分析

上述只是通过简单的示例来实现下java的内置的SPI功能。其实现原理是ServiceLoader是Java内置的用于查找服务提供接口的工具类,通过调用load()方法实现对服务提供接口的查找,最后遍历来逐个访问服务提供接口的实现类。

public final class ServiceLoader
    implements Iterable
{

    private static final String PREFIX = "META-INF/services/";

    // The class or interface representing the service being loaded
    private final Class service;

    // The class loader used to locate, load, and instantiate providers
    private final ClassLoader loader;

    // The access control context taken when the ServiceLoader is created
    private final AccessControlContext acc;

    // Cached providers, in instantiation order
    private LinkedHashMap providers = new LinkedHashMap<>();

    // The current lazy-lookup iterator
    private LazyIterator lookupIterator;
    
 }

从源码可以发现:

  • ServiceLoader类本身实现了Iterable接口并实现了其中的iterator方法,iterator方法的实现中调用了LazyIterator这个内部类中的方法,迭代器创建实例。

  • 所有服务提供接口的对应文件都是放置在META-INF/services/目录下,final类型决定了PREFIX目录不可变更。

虽然java提供的SPI机制的思想非常好,但是也存在相应的弊端。具体如下:

  • Java内置的方法方式只能通过遍历来获取
  • 服务提供接口必须放到META-INF/services/目录下。

针对java的spi存在的问题,Spring的SPI机制沿用的SPI的思想,但对其进行扩展和优化。

Spring SPI

Spring SPI沿用了Java SPI的设计思想,Spring采用的是spring.factories方式实现SPI机制,可以在不修改Spring源码的前提下,提供Spring框架的扩展性。

Spring 示例

定义接口

public interface DataBaseSPI
{
   void getConnection();
}

相关实现
#DB2实现

public class DB2DataBase implements DataBaseSPI
{
    @Override
    public void getConnection()
    {
        System.out.println("this database is db2");
    }

}

#Mysql实现

public class MysqlDataBase implements DataBaseSPI
{
    @Override
    public void getConnection()
    {
       System.out.println("this is mysql database");
    }

}

1.在项目的META-INF目录下,新增spring.factories文件,内容如下:

cn.fntop.springspi.DataBaseSPI=cn.fntop.springspi.DB2DataBase,cn.fntop.springspi.MysqlDataBase

说明多个实现采用逗号分隔

相关测试类

public class SpringSPITest
{
    public static void main(String[] args)
    {
         List dataBaseSPIs =SpringFactoriesLoader.loadFactories(DataBaseSPI.class, 
                 Thread.currentThread().getContextClassLoader());
         
         for(DataBaseSPI datBaseSPI:dataBaseSPIs){
            datBaseSPI.getConnection();
         }
    }
}

输出结果

this database is db2
this is mysql database

从示例中我们看出,Spring 采用spring.factories实现SPI与java实现SPI非常相似,但是spring的spi方式针对java的spi进行的相关优化具体内容如下:

  • Java SPI是一个服务提供接口对应一个配置文件,配置文件中存放当前接口的所有实现类,多个服务提供接口对应多个配置文件,所有配置都在services目录下;
  • Spring factories SPI是一个spring.factories配置文件存放多个接口及对应的实现类,以接口全限定名作为key,实现类作为value来配置,多个实现类用逗号隔开,仅spring.factories一个配置文件。

那么spring是如何通过加载spring.factories来实现SpI的呢?我们可以通过源码来进一步分析。

源码分析

public static  List loadFactories(Class factoryType, @Nullable ClassLoader classLoader) {
		Assert.notNull(factoryType, "'factoryType' must not be null");
		ClassLoader classLoaderToUse = classLoader;
		if (classLoaderToUse == null) {
        //确定类加载器
			classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
		}
        //核心逻辑,加载meta-info下的文件
		List factoryImplementationNames = loadFactoryNames(factoryType, classLoaderToUse);
		if (logger.isTraceEnabled()) {
			logger.trace("Loaded [" + factoryType.getName() + "] names: " + factoryImplementationNames);
		}
		List result = new ArrayList<>(factoryImplementationNames.size());
		for (String factoryImplementationName : factoryImplementationNames) {
			result.add(instantiateFactory(factoryImplementationName, factoryType, classLoaderToUse));
		}
		AnnotationAwareOrderComparator.sort(result);
		return result;
	}

说明:loadFactoryNames解析spring.factories文件中指定接口的实现类的全限定名,具体实现如下:

private static Map> loadSpringFactories(ClassLoader classLoader) {
		Map> result = cache.get(classLoader);
		if (result != null) {
			return result;
		}

		result = new HashMap<>();
		try {
        //加载所有jar中META-INFO/spring.factories文件路径并以枚举值返回
			Enumeration urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
			while (urls.hasMoreElements()) {
				URL url = urls.nextElement();
				UrlResource resource = new UrlResource(url);
				Properties properties = PropertiesLoaderUtils.loadProperties(resource);
				for (Map.Entry entry : properties.entrySet()) {
					String factoryTypeName = ((String) entry.getKey()).trim();
					String[] factoryImplementationNames =
							StringUtils.commaDelimitedListToStringArray((String) entry.getValue());
					for (String factoryImplementationName : factoryImplementationNames) {
						result.computeIfAbsent(factoryTypeName, key -> new ArrayList<>())
								.add(factoryImplementationName.trim());
					}
				}
			}

			// Replace all lists with unmodifiable lists containing unique elements
			result.replaceAll((factoryType, implementations) -> implementations.stream().distinct()
					.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));
			cache.put(classLoader, result);
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Unable to load factories from location [" +
					FACTORIES_RESOURCE_LOCATION + "]", ex);
		}
		return result;
	}

获取所有jar包中META-INF/spring.factories文件路径,以枚举值返回。
遍历spring.factories文件路径,逐个加载解析,整合factoryClass类型的实现类名称,获取到实现类的全类名称后进行类的实例化操作,其相关源码如下:

private static  T instantiateFactory(String factoryImplementationName, Class factoryType, ClassLoader classLoader) {
		try {
			Class factoryImplementationClass = ClassUtils.forName(factoryImplementationName, classLoader);
			if (!factoryType.isAssignableFrom(factoryImplementationClass)) {
				throw new IllegalArgumentException(
						"Class [" + factoryImplementationName + "] is not assignable to factory type [" + factoryType.getName() + "]");
			}
			return (T) ReflectionUtils.accessibleConstructor(factoryImplementationClass).newInstance();
		}
		catch (Throwable ex) {
			throw new IllegalArgumentException(
				"Unable to instantiate factory class [" + factoryImplementationName + "] for factory type [" + factoryType.getName() + "]",
				ex);
		}
	}

实例化是通过反射来实现对应的初始化。

总结

本文详细的讲解了java和Spring的SPI机制,SPI技术将服务接口与服务实现进行分离实现解耦,从而提升程序的可扩展性。如有疑问,请留言。

原文链接

你可能感兴趣的:(java,spring,boot)