11/*
2- * Copyright 2012 the original author or authors.
2+ * Copyright 2012-2016 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616package org .springframework .plugin .core .config ;
1717
1818import org .springframework .beans .factory .annotation .Qualifier ;
19- import org .springframework .beans .factory .support .AbstractBeanDefinition ;
2019import org .springframework .beans .factory .support .AutowireCandidateQualifier ;
2120import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
2221import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
22+ import org .springframework .beans .factory .support .RootBeanDefinition ;
2323import org .springframework .context .annotation .ImportBeanDefinitionRegistrar ;
24+ import org .springframework .core .ResolvableType ;
2425import org .springframework .core .type .AnnotationMetadata ;
26+ import org .springframework .plugin .core .OrderAwarePluginRegistry ;
27+ import org .springframework .plugin .core .Plugin ;
28+ import org .springframework .plugin .core .PluginRegistry ;
2529import org .springframework .plugin .core .support .PluginRegistryFactoryBean ;
30+ import org .springframework .util .Assert ;
2631import org .springframework .util .StringUtils ;
2732
2833/**
@@ -41,15 +46,17 @@ public class PluginRegistriesBeanDefinitionRegistrar implements ImportBeanDefini
4146@ Override
4247public void registerBeanDefinitions (AnnotationMetadata importingClassMetadata , BeanDefinitionRegistry registry ) {
4348
44- Class <?>[] types = (Class <?>[]) importingClassMetadata . getAnnotationAttributes (
45- EnablePluginRegistries .class .getName ()).get ("value" );
49+ Class <?>[] types = (Class <?>[]) importingClassMetadata
50+ . getAnnotationAttributes ( EnablePluginRegistries .class .getName ()).get ("value" );
4651
4752for (Class <?> type : types ) {
4853
4954BeanDefinitionBuilder builder = BeanDefinitionBuilder .rootBeanDefinition (PluginRegistryFactoryBean .class );
5055builder .addPropertyValue ("type" , type );
5156
52- AbstractBeanDefinition beanDefinition = builder .getBeanDefinition ();
57+ RootBeanDefinition beanDefinition = (RootBeanDefinition ) builder .getBeanDefinition ();
58+ beanDefinition .setTargetType (getTargetType (type ));
59+
5360Qualifier annotation = type .getAnnotation (Qualifier .class );
5461
5562// If the plugin interface has a Qualifier annotation, propagate that to the bean definition of the registry
@@ -60,9 +67,25 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
6067}
6168
6269// Default
63- String beanName = annotation == null ? StringUtils .uncapitalize (type .getSimpleName () + "Registry" ) : annotation
64- .value ();
70+ String beanName = annotation == null ? StringUtils .uncapitalize (type .getSimpleName () + "Registry" )
71+ : annotation .value ();
6572registry .registerBeanDefinition (beanName , builder .getBeanDefinition ());
6673}
6774}
75+
76+ /**
77+ * Returns the target type of the {@link PluginRegistry} for the given plugin type.
78+ *
79+ * @param pluginType must not be {@literal null}.
80+ * @return
81+ */
82+ private static ResolvableType getTargetType (Class <?> pluginClass ) {
83+
84+ Assert .notNull (pluginClass , "Plugin type must not be null!" );
85+
86+ ResolvableType delimiterType = ResolvableType .forClass (Plugin .class , pluginClass ).getGeneric (0 );
87+ ResolvableType pluginType = ResolvableType .forClass (pluginClass );
88+
89+ return ResolvableType .forClassWithGenerics (OrderAwarePluginRegistry .class , pluginType , delimiterType );
90+ }
6891}
0 commit comments