最近有接触到Plugin这个插件,它是世界上最小规模的插件系统,不像spring应用广泛,用plugin主要是做策略模式,觉得很好用,最近做下总结。Spring Plugin通过提供Plugin接口实现的核心灵活性,可以满足构建模块化可扩展应用程序的要求。
添加maven依赖
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
Spring Plugin提供一个Plugin接口供后续继承使用声明,然后通过@EnablePluginRegistries注解依赖注入到Spring的容器中,Spring容器会为我们自动匹配到插件的所有实现子对象,通过依赖注入,注入PluginRegistry对象拿到插件实例进行操作。
1、定义一个服务接口,接口必须继承Plugin接口
2、定义两个服务实现类。
3、主要在于EnablePluginRegistries注解,用来注入PluginRegistry
spring-plugin会帮我们向IOC容器自动注入PluginRegistry,我们通过它来获取plugin。
EnablePluginRegistries注解原码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Import(PluginRegistriesBeanDefinitionRegistrar.class)
public @interface EnablePluginRegistries {
/**
* The {@link Plugin} types to register {@link PluginRegistry} instances for. The registries will be named after the
* uncapitalized plugin type extended with {@code Registry}. So for a plugin interface {@code SamplePlugin} the
* exposed bean name will be {@code samplePluginRegistry}. This can be used on the client side to make sure you get
* the right {@link PluginRegistry} injected by using the {@link Qualifier} annotation and referring to that bean
* name. If the auto-generated bean name collides with one already in your application you can use the
* {@link Qualifier} annotation right at the plugin interface to define a custom name.
*
* @return
*/
Class<? extends Plugin<?>>[] value();
}
PluginRegistriesBeanDefinitionRegistrar 实现:
public class PluginRegistriesBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
private static final Logger LOG = LoggerFactory.getLogger(PluginRegistriesBeanDefinitionRegistrar.class);
/*
* (non-Javadoc)
* @see org.springframework.context.annotation.ImportBeanDefinitionRegistrar#registerBeanDefinitions(org.springframework.core.type.AnnotationMetadata, org.springframework.beans.factory.support.BeanDefinitionRegistry)
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
Map<String, Object> annotationAttributes = importingClassMetadata
.getAnnotationAttributes(EnablePluginRegistries.class.getName());
if (annotationAttributes == null) {
LOG.info("No EnablePluginRegistries annotation found on type {}!", importingClassMetadata.getClassName());
return;
}
Class<?>[] types = (Class<?>[]) annotationAttributes.get("value");
for (Class<?> type : types) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(PluginRegistryFactoryBean.class);
builder.addPropertyValue("type", type);
RootBeanDefinition beanDefinition = (RootBeanDefinition) builder.getBeanDefinition();
beanDefinition.setTargetType(getTargetType(type));
Qualifier annotation = type.getAnnotation(Qualifier.class);
// If the plugin interface has a Qualifier annotation, propagate that to the bean definition of the registry
if (annotation != null) {
AutowireCandidateQualifier qualifierMetadata = new AutowireCandidateQualifier(Qualifier.class);
qualifierMetadata.setAttribute(AutowireCandidateQualifier.VALUE_KEY, annotation.value());
beanDefinition.addQualifier(qualifierMetadata);
}
// Default
String beanName = annotation == null //
? StringUtils.uncapitalize(type.getSimpleName() + "Registry") //
: annotation.value();
registry.registerBeanDefinition(beanName, builder.getBeanDefinition());
}
}
/**
* Returns the target type of the {@link PluginRegistry} for the given plugin type.
*
* @param pluginType must not be {@literal null}.
* @return
*/
private static ResolvableType getTargetType(Class<?> pluginClass) {
Assert.notNull(pluginClass, "Plugin type must not be null!");
ResolvableType delimiterType = ResolvableType.forClass(Plugin.class, pluginClass).getGeneric(0);
ResolvableType pluginType = ResolvableType.forClass(pluginClass);
return ResolvableType.forClassWithGenerics(OrderAwarePluginRegistry.class, pluginType, delimiterType);
}
}
自定义返回的几种方法
1、返回第一个Plugin支持给定的分隔符,或者给定懒洋洋地提供的插件,如果没有可以找到。
参数:分隔符——可以为空。defaultSupplier – 不能为空。
返回:如果没有找到支持给定分隔符或给定延迟提供的Plugin的单个Plugin 。
T getPluginOrDefaultFor(S delimiter, Supplier defaultSupplier);
2、返回给定分隔符的所有插件。
参数:delimiter – 不能为空。
返回:插件列表或空列表(如果没有找到)
List getPluginsFor(S delimiter);
3、返回为给定分隔符找到的第一个Plugin 。 因此,进一步配置的Plugin被忽略。
参数:delimiter – 不能为空。
回报:给定原始系统的Plugin或Optional.empty()如果没有找到。
抛出:IllegalArgumentException - 如果给定分隔符没有Plugin
T getRequiredPluginFor(S delimiter) throws IllegalArgumentException;