GH-2566 Make test binder an auto-configuration

Resolves #2566
This commit is contained in:
Oleg Zhurakousky
2022-12-07 14:15:28 -08:00
parent 764a90720a
commit 474ee07086
5 changed files with 13 additions and 19 deletions

View File

@@ -0,0 +1,2 @@
integration:\
org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration

View File

@@ -341,7 +341,6 @@ public class DefaultBinderFactory implements BinderFactory, DisposableBean, Appl
* @param configurationName binder configuration name
* @return binder child application context that has not been refreshed
*/
@SuppressWarnings("rawtypes")
ConfigurableApplicationContext createBinderContextForAOT(String configurationName) {
logger.info("Pre-creating binder child context (AOT) for " + configurationName);
BinderConfiguration binderConfiguration = this.binderConfigurations.get(configurationName);
@@ -411,7 +410,7 @@ public class DefaultBinderFactory implements BinderFactory, DisposableBean, Appl
binderProducingContext.setParent(this.context);
}
else if (this.context != null) {
this.propagateSharedBeans(binderProducingContext);
this.propagateSharedBeans((GenericApplicationContext) this.context, binderProducingContext);
binderProducingContext.addApplicationListener(new ApplicationListener<ApplicationEvent>() {
@Override
public void onApplicationEvent(ApplicationEvent event) {
@@ -444,12 +443,14 @@ public class DefaultBinderFactory implements BinderFactory, DisposableBean, Appl
if (refresh) {
binderProducingContext.refresh();
}
if ("integration".equals(binderType.getDefaultName())) {
this.propagateSharedBeans((GenericApplicationContext) this.context, binderProducingContext);
}
return binderProducingContext;
}
private void propagateSharedBeans(GenericApplicationContext binderProducingContext) {
GenericConversionService binderProducingConversionService = (GenericConversionService) binderProducingContext.getBeanFactory().getConversionService();
private void propagateSharedBeans(GenericApplicationContext toContext, GenericApplicationContext fromContext) {
GenericConversionService binderProducingConversionService = (GenericConversionService) toContext.getBeanFactory().getConversionService();
try {
Enumeration<URL> resources = ClassUtils.getDefaultClassLoader().getResources("META-INF/shared.beans");
while (resources.hasMoreElements()) {
@@ -460,14 +461,14 @@ public class DefaultBinderFactory implements BinderFactory, DisposableBean, Appl
for (Object className : classNames) {
Class<Object> beanType = this.loadClass(((String) className).trim());
if (beanType != null) {
Map<String, Object> beansOfType = this.context.getBeansOfType(beanType);
Map<String, Object> beansOfType = fromContext.getBeansOfType(beanType);
beansOfType.entrySet().stream().forEach(entry -> {
Object bean = entry.getValue();
if (bean instanceof Converter) {
binderProducingConversionService.addConverter((Converter<?, ?>) bean);
}
else {
binderProducingContext.registerBean(entry.getKey() + "_child", beanType, () -> entry.getValue());
toContext.registerBean(entry.getKey() + "_child", beanType, () -> entry.getValue());
}
});
}

View File

@@ -173,16 +173,6 @@ public class BinderFactoryAutoConfiguration {
try {
Enumeration<URL> resources = classLoader.getResources("META-INF/spring.binders");
// see if test binder is available on the classpath and if so add it to the binderTypes
try {
BinderType bt = new BinderType("integration", new Class[] {
classLoader.loadClass("org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration")});
binderTypes.put("integration", bt);
}
catch (Exception e) {
// ignore. means test binder is not available
}
if (binderTypes.isEmpty() && !Boolean.valueOf(this.selfContained)
&& (resources == null || !resources.hasMoreElements())) {
this.logger.debug(
@@ -198,7 +188,6 @@ public class BinderFactoryAutoConfiguration {
}
}
}
}
catch (IOException | ClassNotFoundException e) {
throw new BeanCreationException("Cannot create binder factory:", e);

View File

@@ -141,7 +141,7 @@ public class BindingServiceConfiguration {
if (!existingBinderConfigurations.contains(binderEntry.getKey())) {
binderConfigurations.put(binderEntry.getKey(),
new BinderConfiguration(binderEntry.getKey(), new HashMap<>(),
true, !"integration".equals(binderEntry.getKey())));
true, true)); // true, !"integration".equals(binderEntry.getKey())));
}
}
}

View File

@@ -10,3 +10,5 @@ org.springframework.kafka.config.KafkaStreamsCustomizer
org.springframework.rabbit.stream.listener.ConsumerCustomizer
org.springframework.amqp.core.DeclarableCustomizer
org.springframework.cloud.stream.config.ProducerMessageHandlerCustomizer
org.springframework.cloud.stream.binder.test.InputDestination
org.springframework.cloud.stream.binder.test.OutputDestination