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:
Artem Bilan
2019-03-01 10:26:05 -05:00
committed by Gary Russell
parent 4365eaef6d
commit 3657d05596
27 changed files with 345 additions and 253 deletions

View File

@@ -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));
}
}

View File

@@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<int:inbound-channel-adapter id="inboundChannelAdapter" channel="results">
<bean class="org.springframework.integration.test.mock.MockIntegration" factory-method="mockMessageSource">

View File

@@ -57,7 +57,7 @@ import org.springframework.test.context.junit4.SpringRunner;
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MockMessageSourceTests.Config.class)
@SpringIntegrationTest(noAutoStartup = {"inboundChannelAdapter", "*Source*"})
@SpringIntegrationTest(noAutoStartup = { "inboundChannelAdapter", "*Source*" })
@DirtiesContext
public class MockMessageSourceTests {
@@ -76,7 +76,7 @@ public class MockMessageSourceTests {
@After
public void tearDown() {
this.mockIntegrationContext.resetBeans("mySourceEndpoint", "inboundChannelAdapter");
results.purge(null);
this.results.purge(null);
}
@Test
@@ -103,8 +103,11 @@ public class MockMessageSourceTests {
@Test
public void testMockMessageSourceInConfig() {
this.applicationContext.getBean("mockMessageSourceTests.Config.testingMessageSource.inboundChannelAdapter",
Lifecycle.class).start();
Lifecycle channelAdapter =
this.applicationContext
.getBean("mockMessageSourceTests.Config.testingMessageSource.inboundChannelAdapter",
Lifecycle.class);
channelAdapter.start();
Message<?> receive = this.results.receive(10_000);
assertThat(receive).isNotNull();
@@ -120,13 +123,13 @@ public class MockMessageSourceTests {
assertThat(receive.getPayload()).isEqualTo(3);
}
this.applicationContext.getBean("mockMessageSourceTests.Config.testingMessageSource.inboundChannelAdapter",
Lifecycle.class).stop();
channelAdapter.stop();
}
@Test
public void testMockMessageSourceInXml() {
this.applicationContext.getBean("inboundChannelAdapter", Lifecycle.class).start();
Lifecycle channelAdapter = this.applicationContext.getBean("inboundChannelAdapter", Lifecycle.class);
channelAdapter.start();
Message<?> receive = this.results.receive(10_000);
assertThat(receive).isNotNull();
@@ -142,7 +145,7 @@ public class MockMessageSourceTests {
assertThat(receive.getPayload()).isEqualTo("c");
}
this.applicationContext.getBean("inboundChannelAdapter", Lifecycle.class).stop();
channelAdapter.stop();
}
@Test
@@ -198,6 +201,11 @@ public class MockMessageSourceTests {
return Pollers.fixedDelay(10);
}
@Bean
public QueueChannel results() {
return new QueueChannel();
}
@Bean
public IntegrationFlow myFlow() {
return IntegrationFlows
@@ -208,11 +216,6 @@ public class MockMessageSourceTests {
.get();
}
@Bean
public QueueChannel results() {
return new QueueChannel();
}
@InboundChannelAdapter(channel = "results")
@Bean
public MessageSource<Integer> testingMessageSource() {