Skip to content

Commit a800aa0

Browse files
committed
spring-projects#18 - Explicitly declare bean definitions target type for plugin registries.
We now use the API introduced in Spring Framework 4.3.3 to explicitly declare a ResolvableType for the BeanDefinition of the plugin registries. This allows to autowire registries as PluginRegisty<Foo, …> and PluginRegistry<Bar, …> without using explicit qualifiers.
1 parent 1feb021 commit a800aa0

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

core/src/main/java/org/springframework/plugin/core/config/PluginRegistriesBeanDefinitionRegistrar.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -16,13 +16,18 @@
1616
package org.springframework.plugin.core.config;
1717

1818
import org.springframework.beans.factory.annotation.Qualifier;
19-
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2019
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
2120
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2221
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
22+
import org.springframework.beans.factory.support.RootBeanDefinition;
2323
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
24+
import org.springframework.core.ResolvableType;
2425
import 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;
2529
import org.springframework.plugin.core.support.PluginRegistryFactoryBean;
30+
import org.springframework.util.Assert;
2631
import org.springframework.util.StringUtils;
2732

2833
/**
@@ -41,15 +46,17 @@ public class PluginRegistriesBeanDefinitionRegistrar implements ImportBeanDefini
4146
@Override
4247
public 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

4752
for (Class<?> type : types) {
4853

4954
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(PluginRegistryFactoryBean.class);
5055
builder.addPropertyValue("type", type);
5156

52-
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
57+
RootBeanDefinition beanDefinition = (RootBeanDefinition) builder.getBeanDefinition();
58+
beanDefinition.setTargetType(getTargetType(type));
59+
5360
Qualifier 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();
6572
registry.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
}

core/src/test/java/org/springframework/plugin/core/config/EnablePluginRegistriesIntegrationTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -50,19 +50,15 @@ public SamplePluginImplementation pluginImpl() {
5050
}
5151
}
5252

53-
@Autowired
54-
@Qualifier("samplePluginRegistry")
55-
PluginRegistry<SamplePlugin, String> registry;
53+
@Autowired PluginRegistry<SamplePlugin, String> registry;
5654

5755
@Test
5856
public void registersPluginRegistries() {
5957
assertThat(registry, is(notNullValue()));
6058
}
6159

6260
@Qualifier("myQualifier")
63-
interface AnotherPlugin extends Plugin<String> {
64-
65-
}
61+
interface AnotherPlugin extends Plugin<String> {}
6662

6763
static class AnotherSamplePluginImplementation implements AnotherPlugin {
6864

0 commit comments

Comments
 (0)