Defer Messaging annotations process (#2769)
* Defer Messaging annotations process The `AbstractMethodAnnotationPostProcessor` and its implementations have a `beanFactory.getBean()` call for the `@Bean` methods with Messaging annotations. This is done, actually, from the `MessagingAnnotationPostProcessor.postProcessAfterInitialization()` which might be still too early in some scenarios, like Spring Cloud Feign with its child application contexts being initialized from the `FeignClientFactoryBean`, causing a `BeanCurrentlyInCreationException` See https://stackoverflow.com/questions/54887963/beancurrentlyincreationexception-when-using-spring-integration-with-spring-cloud * Implement a `SmartInitializingSingleton` for the `MessagingAnnotationPostProcessor` and gather `Runnable` wrappers for newly introduced `postProcessMethodAndRegisterEndpointIfAny()` to be called later in the `afterSingletonsInstantiated()` when context is still in the initialization phase. All runtime-registered beans are going to be processed normally from the regular `postProcessAfterInitialization()` **Cherry-pick to 5.1.x** * * Fix unused imports in the `MessagingAnnotationsWithBeanAnnotationTests` * * Fix `IntegrationEndpointsInitializer` in the testing framework to handle all the possible `AbstractEndpoint` beans registration. See its JavaDocs for more info * Fix `AbstractCorrelatingMessageHandlerParser` and `AbstractConsumerEndpointParser` to use bean names for `outputChannel` and `discardChannel` instead of bean references. Since `MessagingAnnotationPostProcessor` now registers endpoints and beans for channels much later, than parsers, we can't rely on bean references any more there. * Fixes for failing tests which expected `outputChannel/discardChannel` bean references, when it is already just their names for late binding. * Apply some code style polishing for the affected classes. * Add `@Nullable` for `MessageSelector` parameter in the `QueueChannel.purge()`
This commit is contained in:
committed by
Gary Russell
parent
4365eaef6d
commit
3657d05596
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,12 +16,13 @@
|
||||
|
||||
package org.springframework.integration.test.context;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.integration.endpoint.AbstractEndpoint;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -31,19 +32,30 @@ import org.springframework.util.PatternMatchUtils;
|
||||
* A component to customize {@link AbstractEndpoint} beans according
|
||||
* to the provided options in the {@link SpringIntegrationTest} annotation
|
||||
* after all beans are registered in the application context but before its refresh.
|
||||
* <p>
|
||||
* This class implements both {@link SmartInitializingSingleton} and {@link BeanPostProcessor}
|
||||
* to cover all the possible variants of {@link AbstractEndpoint} beans registration.
|
||||
* First of all a bean for this class is registered in the application context, when XML configurations
|
||||
* are parsed and registered already by the Spring Testing Framework, therefore a
|
||||
* {@link BeanPostProcessor#postProcessBeforeInitialization(Object, String)} hook is not called for those beans.
|
||||
* On the other hand we can't always rely on just a {@link SmartInitializingSingleton#afterSingletonsInstantiated()}
|
||||
* because {@link SmartInitializingSingleton} beans are not ordered and some implementations may register beans
|
||||
* later, than this {@link #afterSingletonsInstantiated()} is called.
|
||||
* Plus beans might be registered at runtime, therefore {@link #postProcessBeforeInitialization(Object, String)}
|
||||
* is still applied.
|
||||
*
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
class IntegrationEndpointsInitializer implements SmartInitializingSingleton, BeanFactoryAware {
|
||||
class IntegrationEndpointsInitializer implements SmartInitializingSingleton, BeanPostProcessor, BeanFactoryAware {
|
||||
|
||||
private final SpringIntegrationTest springIntegrationTest;
|
||||
private final String[] patterns;
|
||||
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
IntegrationEndpointsInitializer(SpringIntegrationTest springIntegrationTest) {
|
||||
this.springIntegrationTest = springIntegrationTest;
|
||||
this.patterns = springIntegrationTest.noAutoStartup();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -53,22 +65,25 @@ class IntegrationEndpointsInitializer implements SmartInitializingSingleton, Bea
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterSingletonsInstantiated() {
|
||||
Map<String, AbstractEndpoint> endpoints = this.beanFactory.getBeansOfType(AbstractEndpoint.class);
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (bean instanceof AbstractEndpoint && ((AbstractEndpoint) bean).isAutoStartup() && match(beanName)) {
|
||||
((AbstractEndpoint) bean).setAutoStartup(false);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
endpoints.entrySet()
|
||||
@Override
|
||||
public void afterSingletonsInstantiated() {
|
||||
this.beanFactory.getBeansOfType(AbstractEndpoint.class)
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> match(entry.getKey()))
|
||||
.filter(entry -> entry.getValue().isAutoStartup() && match(entry.getKey()))
|
||||
.forEach(entry -> entry.getValue().setAutoStartup(false));
|
||||
}
|
||||
|
||||
private boolean match(String name) {
|
||||
for (String pattern : this.springIntegrationTest.noAutoStartup()) {
|
||||
if (PatternMatchUtils.simpleMatch(pattern, name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return Arrays.stream(this.patterns)
|
||||
.anyMatch(pattern -> PatternMatchUtils.simpleMatch(pattern, name));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user