GH-8611: Extract MessagingAnnotationBeanPostProcessor (#8661)

Fixes https://github.com/spring-projects/spring-integration/issues/8611

According to the latest Spring Framework requirements related to AOT the
`BeanDefinitionRegistryPostProcessor` must not do anything but only bean registrations

* Extract a `MessagingAnnotationBeanPostProcessor` component with a `BeanPostProcessor` logic
from the `MessagingAnnotationPostProcessor` and leave the last one as just
a `BeanDefinitionRegistryPostProcessor` impl
* Fix tests according to a new logic
This commit is contained in:
Artem Bilan
2023-06-28 08:59:37 -04:00
committed by GitHub
parent 13980af1e1
commit 4583afe0ad
7 changed files with 321 additions and 286 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -20,7 +20,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
@@ -55,6 +54,7 @@ public class DirectChannelSubscriptionTests {
@BeforeEach
public void setupChannels() {
this.context.registerBean(MessagingAnnotationPostProcessor.class);
this.context.registerChannel("sourceChannel", this.sourceChannel);
this.context.registerChannel("targetChannel", this.targetChannel);
}
@@ -80,13 +80,9 @@ public class DirectChannelSubscriptionTests {
@Test
public void sendAndReceiveForAnnotatedEndpoint() {
MessagingAnnotationPostProcessor postProcessor = new MessagingAnnotationPostProcessor();
postProcessor.postProcessBeanDefinitionRegistry((BeanDefinitionRegistry) this.context.getBeanFactory());
postProcessor.postProcessBeanFactory(this.context.getBeanFactory());
postProcessor.afterSingletonsInstantiated();
TestEndpoint endpoint = new TestEndpoint();
postProcessor.postProcessAfterInitialization(endpoint, "testEndpoint");
this.context.registerEndpoint("testEndpoint", new TestEndpoint());
this.context.refresh();
this.sourceChannel.send(new GenericMessage<>("foo"));
Message<?> response = this.targetChannel.receive();
assertThat(response.getPayload()).isEqualTo("foo-from-annotated-endpoint");
@@ -113,11 +109,7 @@ public class DirectChannelSubscriptionTests {
public void exceptionThrownFromAnnotatedEndpoint() {
QueueChannel errorChannel = new QueueChannel();
this.context.registerChannel(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, errorChannel);
MessagingAnnotationPostProcessor postProcessor = new MessagingAnnotationPostProcessor();
postProcessor.postProcessBeanFactory(this.context.getBeanFactory());
postProcessor.afterSingletonsInstantiated();
FailingTestEndpoint endpoint = new FailingTestEndpoint();
postProcessor.postProcessAfterInitialization(endpoint, "testEndpoint");
this.context.registerEndpoint("testEndpoint", new FailingTestEndpoint());
this.context.refresh();
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> this.sourceChannel.send(new GenericMessage<>("foo")));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -23,11 +23,11 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.integration.annotation.Filter;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.MessagingAnnotationBeanPostProcessor;
import org.springframework.integration.config.MessagingAnnotationPostProcessor;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
@@ -50,19 +50,15 @@ public class FilterAnnotationPostProcessorTests {
private final TestApplicationContext context = TestUtils.createTestApplicationContext();
private final MessagingAnnotationPostProcessor postProcessor = new MessagingAnnotationPostProcessor();
private final DirectChannel inputChannel = new DirectChannel();
private final QueueChannel outputChannel = new QueueChannel();
@BeforeEach
public void init() {
this.context.registerBean(MessagingAnnotationPostProcessor.class);
this.context.registerChannel("input", this.inputChannel);
this.context.registerChannel("output", this.outputChannel);
this.postProcessor.postProcessBeanDefinitionRegistry((BeanDefinitionRegistry) this.context.getBeanFactory());
this.postProcessor.postProcessBeanFactory(this.context.getBeanFactory());
this.postProcessor.afterSingletonsInstantiated();
}
@AfterEach
@@ -175,21 +171,26 @@ public class FilterAnnotationPostProcessorTests {
@Test
public void invalidMethodWithStringReturnType() {
Object filter = new TestFilterWithStringReturnType();
context.refresh();
var postProcessor = context.getBean(MessagingAnnotationBeanPostProcessor.class);
assertThatIllegalArgumentException()
.isThrownBy(() -> this.postProcessor.postProcessAfterInitialization(filter, "testFilter"));
.isThrownBy(() ->
postProcessor.postProcessAfterInitialization(
new TestFilterWithStringReturnType(), "testFilter"));
}
@Test
public void invalidMethodWithVoidReturnType() {
Object filter = new TestFilterWithVoidReturnType();
context.refresh();
var postProcessor = context.getBean(MessagingAnnotationBeanPostProcessor.class);
assertThatIllegalArgumentException()
.isThrownBy(() -> postProcessor.postProcessAfterInitialization(filter, "testFilter"));
.isThrownBy(() ->
postProcessor.postProcessAfterInitialization(new TestFilterWithVoidReturnType(), "testFilter"));
}
private void testValidFilter(Object filter) {
postProcessor.postProcessAfterInitialization(filter, "testFilter");
context.registerEndpoint("testFilter", filter);
context.refresh();
inputChannel.send(new GenericMessage<>("good"));
Message<?> passed = outputChannel.receive(0);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -27,8 +27,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.annotation.MessageEndpoint;
@@ -36,6 +34,7 @@ import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.MessagingAnnotationBeanPostProcessor;
import org.springframework.integration.config.MessagingAnnotationPostProcessor;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
@@ -63,9 +62,10 @@ public class MessagingAnnotationPostProcessorTests {
TestApplicationContext context = TestUtils.createTestApplicationContext();
DirectChannel inputChannel = new DirectChannel();
context.registerChannel("inputChannel", inputChannel);
context.registerBean(MessagingAnnotationPostProcessor.class);
context.refresh();
MessagingAnnotationPostProcessor postProcessor = prepareMessagingAnnotationPostProcessor(context);
MessagingAnnotationBeanPostProcessor postProcessor = context.getBean(MessagingAnnotationBeanPostProcessor.class);
ServiceActivatorAnnotatedBean bean = new ServiceActivatorAnnotatedBean();
postProcessor.postProcessAfterInitialization(bean, "testBean");
assertThat(context.containsBean("testBean.test.serviceActivator")).isTrue();
@@ -135,11 +135,11 @@ public class MessagingAnnotationPostProcessorTests {
@Test
public void outboundOnlyServiceActivator() throws InterruptedException {
TestApplicationContext context = TestUtils.createTestApplicationContext();
context.registerBean(MessagingAnnotationPostProcessor.class);
context.registerChannel("testChannel", new DirectChannel());
MessagingAnnotationPostProcessor postProcessor = prepareMessagingAnnotationPostProcessor(context);
CountDownLatch latch = new CountDownLatch(1);
OutboundOnlyTestBean testBean = new OutboundOnlyTestBean(latch);
postProcessor.postProcessAfterInitialization(testBean, "testBean");
context.registerEndpoint("testBean", testBean);
context.refresh();
DestinationResolver<MessageChannel> channelResolver = new BeanFactoryChannelResolver(context);
MessageChannel testChannel = channelResolver.resolveDestination("testChannel");
@@ -153,15 +153,15 @@ public class MessagingAnnotationPostProcessorTests {
@Test
public void testChannelResolution() {
TestApplicationContext context = TestUtils.createTestApplicationContext();
context.registerBean(MessagingAnnotationPostProcessor.class);
DirectChannel inputChannel = new DirectChannel();
QueueChannel outputChannel = new QueueChannel();
DirectChannel eventBus = new DirectChannel();
context.registerChannel("inputChannel", inputChannel);
context.registerChannel("outputChannel", outputChannel);
context.registerChannel("eventBus", eventBus);
MessagingAnnotationPostProcessor postProcessor = prepareMessagingAnnotationPostProcessor(context);
ServiceActivatorAnnotatedBean bean = new ServiceActivatorAnnotatedBean();
postProcessor.postProcessAfterInitialization(bean, "testBean");
context.registerEndpoint("testBean", bean);
context.refresh();
Message<?> message = MessageBuilder.withPayload("test")
.setReplyChannelName("outputChannel").build();
@@ -178,14 +178,14 @@ public class MessagingAnnotationPostProcessorTests {
@Test
public void testProxiedMessageEndpointAnnotation() {
TestApplicationContext context = TestUtils.createTestApplicationContext();
context.registerBean(MessagingAnnotationPostProcessor.class);
DirectChannel inputChannel = new DirectChannel();
QueueChannel outputChannel = new QueueChannel();
context.registerChannel("inputChannel", inputChannel);
context.registerChannel("outputChannel", outputChannel);
MessagingAnnotationPostProcessor postProcessor = prepareMessagingAnnotationPostProcessor(context);
ProxyFactory proxyFactory = new ProxyFactory(new AnnotatedTestService());
Object proxy = proxyFactory.getProxy();
postProcessor.postProcessAfterInitialization(proxy, "proxy");
context.registerEndpoint("proxy", proxy);
context.refresh();
inputChannel.send(new GenericMessage<>("world"));
Message<?> message = outputChannel.receive(1000);
@@ -196,12 +196,12 @@ public class MessagingAnnotationPostProcessorTests {
@Test
public void testMessageEndpointAnnotationInherited() {
TestApplicationContext context = TestUtils.createTestApplicationContext();
context.registerBean(MessagingAnnotationPostProcessor.class);
DirectChannel inputChannel = new DirectChannel();
QueueChannel outputChannel = new QueueChannel();
context.registerChannel("inputChannel", inputChannel);
context.registerChannel("outputChannel", outputChannel);
MessagingAnnotationPostProcessor postProcessor = prepareMessagingAnnotationPostProcessor(context);
postProcessor.postProcessAfterInitialization(new SimpleAnnotatedEndpointSubclass(), "subclass");
context.registerEndpoint("subclass", new SimpleAnnotatedEndpointSubclass());
context.refresh();
inputChannel.send(new GenericMessage<>("world"));
Message<?> message = outputChannel.receive(1000);
@@ -212,14 +212,14 @@ public class MessagingAnnotationPostProcessorTests {
@Test
public void testMessageEndpointAnnotationInheritedWithProxy() {
TestApplicationContext context = TestUtils.createTestApplicationContext();
context.registerBean(MessagingAnnotationPostProcessor.class);
DirectChannel inputChannel = new DirectChannel();
QueueChannel outputChannel = new QueueChannel();
context.registerChannel("inputChannel", inputChannel);
context.registerChannel("outputChannel", outputChannel);
MessagingAnnotationPostProcessor postProcessor = prepareMessagingAnnotationPostProcessor(context);
ProxyFactory proxyFactory = new ProxyFactory(new SimpleAnnotatedEndpointSubclass());
Object proxy = proxyFactory.getProxy();
postProcessor.postProcessAfterInitialization(proxy, "proxy");
context.registerEndpoint("proxy", proxy);
context.refresh();
inputChannel.send(new GenericMessage<>("world"));
Message<?> message = outputChannel.receive(1000);
@@ -230,12 +230,12 @@ public class MessagingAnnotationPostProcessorTests {
@Test
public void testMessageEndpointAnnotationInheritedFromInterface() {
TestApplicationContext context = TestUtils.createTestApplicationContext();
context.registerBean(MessagingAnnotationPostProcessor.class);
DirectChannel inputChannel = new DirectChannel();
QueueChannel outputChannel = new QueueChannel();
context.registerChannel("inputChannel", inputChannel);
context.registerChannel("outputChannel", outputChannel);
MessagingAnnotationPostProcessor postProcessor = prepareMessagingAnnotationPostProcessor(context);
postProcessor.postProcessAfterInitialization(new SimpleAnnotatedEndpointImplementation(), "impl");
context.registerEndpoint("impl", new SimpleAnnotatedEndpointImplementation());
context.refresh();
inputChannel.send(new GenericMessage<>("ABC"));
Message<?> message = outputChannel.receive(1000);
@@ -246,12 +246,12 @@ public class MessagingAnnotationPostProcessorTests {
@Test
public void testMessageEndpointAnnotationInheritedFromInterfaceWithAutoCreatedChannels() {
TestApplicationContext context = TestUtils.createTestApplicationContext();
context.registerBean(MessagingAnnotationPostProcessor.class);
DirectChannel inputChannel = new DirectChannel();
QueueChannel outputChannel = new QueueChannel();
context.registerChannel("inputChannel", inputChannel);
context.registerChannel("outputChannel", outputChannel);
MessagingAnnotationPostProcessor postProcessor = prepareMessagingAnnotationPostProcessor(context);
postProcessor.postProcessAfterInitialization(new SimpleAnnotatedEndpointImplementation(), "impl");
context.registerEndpoint("impl", new SimpleAnnotatedEndpointImplementation());
context.refresh();
inputChannel.send(new GenericMessage<>("ABC"));
Message<?> message = outputChannel.receive(1000);
@@ -262,14 +262,13 @@ public class MessagingAnnotationPostProcessorTests {
@Test
public void testMessageEndpointAnnotationInheritedFromInterfaceWithProxy() {
TestApplicationContext context = TestUtils.createTestApplicationContext();
context.registerBean(MessagingAnnotationPostProcessor.class);
DirectChannel inputChannel = new DirectChannel();
QueueChannel outputChannel = new QueueChannel();
context.registerChannel("inputChannel", inputChannel);
context.registerChannel("outputChannel", outputChannel);
MessagingAnnotationPostProcessor postProcessor = prepareMessagingAnnotationPostProcessor(context);
ProxyFactory proxyFactory = new ProxyFactory(new SimpleAnnotatedEndpointImplementation());
Object proxy = proxyFactory.getProxy();
postProcessor.postProcessAfterInitialization(proxy, "proxy");
context.registerEndpoint("proxy", proxyFactory.getProxy());
context.refresh();
inputChannel.send(new GenericMessage<>("ABC"));
Message<?> message = outputChannel.receive(1000);
@@ -280,13 +279,12 @@ public class MessagingAnnotationPostProcessorTests {
@Test
public void testTransformer() {
TestApplicationContext context = TestUtils.createTestApplicationContext();
context.registerBean(MessagingAnnotationPostProcessor.class);
DirectChannel inputChannel = new DirectChannel();
context.registerChannel("inputChannel", inputChannel);
QueueChannel outputChannel = new QueueChannel();
context.registerChannel("outputChannel", outputChannel);
MessagingAnnotationPostProcessor postProcessor = prepareMessagingAnnotationPostProcessor(context);
TransformerAnnotationTestBean testBean = new TransformerAnnotationTestBean();
postProcessor.postProcessAfterInitialization(testBean, "testBean");
context.registerEndpoint("testBean", new TransformerAnnotationTestBean());
context.refresh();
inputChannel.send(new GenericMessage<>("foo"));
Message<?> reply = outputChannel.receive(0);
@@ -300,16 +298,6 @@ public class MessagingAnnotationPostProcessorTests {
context.close();
}
private static MessagingAnnotationPostProcessor prepareMessagingAnnotationPostProcessor(
ConfigurableApplicationContext context) {
MessagingAnnotationPostProcessor postProcessor = new MessagingAnnotationPostProcessor();
postProcessor.postProcessBeanDefinitionRegistry((BeanDefinitionRegistry) context.getBeanFactory());
postProcessor.postProcessBeanFactory(context.getBeanFactory());
postProcessor.afterSingletonsInstantiated();
return postProcessor;
}
@MessageEndpoint
public static class OutboundOnlyTestBean {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -23,7 +23,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.Router;
import org.springframework.integration.channel.DirectChannel;
@@ -44,8 +43,6 @@ public class RouterAnnotationPostProcessorTests {
private final TestApplicationContext context = TestUtils.createTestApplicationContext();
private final MessagingAnnotationPostProcessor postProcessor = new MessagingAnnotationPostProcessor();
private final DirectChannel inputChannel = new DirectChannel();
private final QueueChannel outputChannel = new QueueChannel();
@@ -59,14 +56,12 @@ public class RouterAnnotationPostProcessorTests {
@BeforeEach
public void init() {
this.context.registerBean(MessagingAnnotationPostProcessor.class);
context.registerChannel("input", inputChannel);
context.registerChannel("output", outputChannel);
context.registerChannel("routingChannel", routingChannel);
context.registerChannel("integerChannel", integerChannel);
context.registerChannel("stringChannel", stringChannel);
this.postProcessor.postProcessBeanDefinitionRegistry((BeanDefinitionRegistry) this.context.getBeanFactory());
this.postProcessor.postProcessBeanFactory(this.context.getBeanFactory());
this.postProcessor.afterSingletonsInstantiated();
}
@AfterEach
@@ -76,8 +71,7 @@ public class RouterAnnotationPostProcessorTests {
@Test
public void testRouter() {
TestRouter testRouter = new TestRouter();
postProcessor.postProcessAfterInitialization(testRouter, "test");
context.registerEndpoint("test", new TestRouter());
context.refresh();
inputChannel.send(new GenericMessage<>("foo"));
Message<?> replyMessage = outputChannel.receive(0);
@@ -87,8 +81,7 @@ public class RouterAnnotationPostProcessorTests {
@Test
public void testRouterWithListParam() {
TestRouter testRouter = new TestRouter();
postProcessor.postProcessAfterInitialization(testRouter, "test");
context.registerEndpoint("test", new TestRouter());
context.refresh();
routingChannel.send(new GenericMessage<>(Collections.singletonList("foo")));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -20,7 +20,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.Lifecycle;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.Splitter;
@@ -50,6 +49,7 @@ public class SplitterAnnotationPostProcessorTests {
@BeforeEach
public void init() {
this.context.registerBean(MessagingAnnotationPostProcessor.class);
this.context.registerChannel("input", this.inputChannel);
this.context.registerChannel("output", this.outputChannel);
}
@@ -61,12 +61,8 @@ public class SplitterAnnotationPostProcessorTests {
@Test
public void testSplitterAnnotation() {
MessagingAnnotationPostProcessor postProcessor = new MessagingAnnotationPostProcessor();
postProcessor.postProcessBeanDefinitionRegistry((BeanDefinitionRegistry) this.context.getBeanFactory());
postProcessor.postProcessBeanFactory(this.context.getBeanFactory());
postProcessor.afterSingletonsInstantiated();
TestSplitter splitter = new TestSplitter();
postProcessor.postProcessAfterInitialization(splitter, "testSplitter");
context.registerEndpoint("testSplitter", splitter);
context.refresh();
inputChannel.send(new GenericMessage<>("this.is.a.test"));
Message<?> message1 = outputChannel.receive(500);