Skip to content

Commit a843dae

Browse files
artembilangaryrussell
authored andcommitted
Remove Disposables abstraction
* Remove `Disposables` since it is a package protected therefore could not be used outside * Remove the logic relevant ot the `Disposables` in the `MessagingAnnotationPostProcessor` logic * Replace a `registerSingleton()` and `initializeBean()` with the proper `registerBeanDefinition()` and `getBean()` usage. This way bean are going to be destroyed properly * Remove `MessagingAnnotationPostProcessorChannelCreationTests` since its mocking logic is too vague and really covered with many other real tests.
1 parent d7f9258 commit a843dae

File tree

5 files changed

+26
-193
lines changed

5 files changed

+26
-193
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/annotation/AbstractMethodAnnotationPostProcessor.java

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636
import org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor;
3737
import org.springframework.aop.support.NameMatchMethodPointcut;
3838
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
39-
import org.springframework.beans.factory.DisposableBean;
4039
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
4140
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
41+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
4242
import org.springframework.beans.factory.support.BeanDefinitionValidationException;
43+
import org.springframework.beans.factory.support.RootBeanDefinition;
4344
import org.springframework.context.annotation.Bean;
4445
import org.springframework.core.GenericTypeResolver;
4546
import org.springframework.core.ResolvableType;
@@ -120,34 +121,27 @@ public abstract class AbstractMethodAnnotationPostProcessor<T extends Annotation
120121

121122
protected final ConfigurableListableBeanFactory beanFactory; // NOSONAR
122123

124+
protected final BeanDefinitionRegistry definitionRegistry; // NOSONAR
125+
123126
protected final ConversionService conversionService; // NOSONAR
124127

125128
protected final DestinationResolver<MessageChannel> channelResolver; // NOSONAR
126129

127130
protected final Class<T> annotationType; // NOSONAR
128131

129-
protected final Disposables disposables; // NOSONAR
130-
131132
@SuppressWarnings(UNCHECKED)
132133
public AbstractMethodAnnotationPostProcessor(ConfigurableListableBeanFactory beanFactory) {
133134
Assert.notNull(beanFactory, "'beanFactory' must not be null");
134135
this.messageHandlerAttributes.add(SEND_TIMEOUT_ATTRIBUTE);
135136
this.beanFactory = beanFactory;
137+
this.definitionRegistry = (BeanDefinitionRegistry) beanFactory;
136138
this.conversionService = this.beanFactory.getConversionService() != null
137139
? this.beanFactory.getConversionService()
138140
: DefaultConversionService.getSharedInstance();
139141
this.channelResolver = ChannelResolverUtils.getChannelResolver(beanFactory);
140142
this.annotationType =
141143
(Class<T>) GenericTypeResolver.resolveTypeArgument(this.getClass(),
142144
MethodAnnotationPostProcessor.class);
143-
Disposables disposablesBean = null;
144-
try {
145-
disposablesBean = beanFactory.getBean(Disposables.class);
146-
}
147-
catch (@SuppressWarnings("unused") Exception e) {
148-
// NOSONAR - only for test cases
149-
}
150-
this.disposables = disposablesBean;
151145
}
152146

153147
@Override
@@ -187,23 +181,19 @@ public Object postProcess(Object bean, String beanName, Method method, List<Anno
187181
}
188182

189183
private MessageHandler registerHandlerBean(String beanName, Method method, final MessageHandler handler) {
190-
MessageHandler handlerBean = handler;
191184
String handlerBeanName = generateHandlerBeanName(beanName, method);
192-
if (handlerBean instanceof ReplyProducingMessageHandlerWrapper
185+
if (handler instanceof ReplyProducingMessageHandlerWrapper
193186
&& StringUtils.hasText(MessagingAnnotationUtils.endpointIdValue(method))) {
194187
handlerBeanName = handlerBeanName + ".wrapper";
195188
}
196-
if (handlerBean instanceof IntegrationObjectSupport) {
197-
((IntegrationObjectSupport) handlerBean).setComponentName(
189+
if (handler instanceof IntegrationObjectSupport) {
190+
((IntegrationObjectSupport) handler).setComponentName(
198191
handlerBeanName.substring(0,
199192
handlerBeanName.indexOf(IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX)));
200193
}
201-
this.beanFactory.registerSingleton(handlerBeanName, handler);
202-
handlerBean = (MessageHandler) this.beanFactory.initializeBean(handlerBean, handlerBeanName);
203-
if (handlerBean instanceof DisposableBean && this.disposables != null) {
204-
this.disposables.add((DisposableBean) handlerBean);
205-
}
206-
return handlerBean;
194+
this.definitionRegistry.registerBeanDefinition(handlerBeanName,
195+
new RootBeanDefinition(MessageHandler.class, () -> handler));
196+
return this.beanFactory.getBean(handlerBeanName, MessageHandler.class);
207197
}
208198

209199
private void orderable(Method method, MessageHandler handler) {
@@ -355,12 +345,9 @@ protected AbstractEndpoint createEndpoint(MessageHandler handler, @SuppressWarni
355345
}
356346
catch (DestinationResolutionException e) {
357347
if (e.getCause() instanceof NoSuchBeanDefinitionException) {
358-
inputChannel = new DirectChannel();
359-
this.beanFactory.registerSingleton(inputChannelName, inputChannel);
360-
inputChannel = (MessageChannel) this.beanFactory.initializeBean(inputChannel, inputChannelName);
361-
if (this.disposables != null) {
362-
this.disposables.add((DisposableBean) inputChannel);
363-
}
348+
this.definitionRegistry.registerBeanDefinition(inputChannelName,
349+
new RootBeanDefinition(DirectChannel.class, DirectChannel::new));
350+
inputChannel = this.beanFactory.getBean(inputChannelName, MessageChannel.class);
364351
}
365352
else {
366353
throw e;

spring-integration-core/src/main/java/org/springframework/integration/config/annotation/Disposables.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

spring-integration-core/src/main/java/org/springframework/integration/config/annotation/InboundChannelAdapterAnnotationPostProcessor.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2525
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
26+
import org.springframework.beans.factory.support.RootBeanDefinition;
2627
import org.springframework.context.annotation.Bean;
2728
import org.springframework.core.annotation.AnnotatedElementUtils;
2829
import org.springframework.core.annotation.AnnotationUtils;
@@ -122,12 +123,9 @@ else if (ClassUtils.KOTLIN_FUNCTION_0_INVOKE_METHOD != null) {
122123
methodInvokingMessageSource.setObject(bean);
123124
methodInvokingMessageSource.setMethod(method);
124125
String messageSourceBeanName = generateHandlerBeanName(beanName, method);
125-
this.beanFactory.registerSingleton(messageSourceBeanName, methodInvokingMessageSource);
126-
messageSource = (MessageSource<?>) this.beanFactory
127-
.initializeBean(methodInvokingMessageSource, messageSourceBeanName);
128-
if (this.disposables != null) {
129-
this.disposables.add(methodInvokingMessageSource);
130-
}
126+
this.definitionRegistry.registerBeanDefinition(messageSourceBeanName,
127+
new RootBeanDefinition(MethodInvokingMessageSource.class, () -> methodInvokingMessageSource));
128+
messageSource = this.beanFactory.getBean(messageSourceBeanName, MessageSource.class);
131129
}
132130
return messageSource;
133131
}

spring-integration-core/src/main/java/org/springframework/integration/config/annotation/MessagingAnnotationPostProcessor.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import org.springframework.integration.annotation.ServiceActivator;
5757
import org.springframework.integration.annotation.Splitter;
5858
import org.springframework.integration.annotation.Transformer;
59-
import org.springframework.integration.context.IntegrationContextUtils;
6059
import org.springframework.integration.endpoint.AbstractEndpoint;
6160
import org.springframework.integration.util.MessagingAnnotationUtils;
6261
import org.springframework.util.Assert;
@@ -104,11 +103,6 @@ protected ConfigurableListableBeanFactory getBeanFactory() {
104103
@Override
105104
public void afterPropertiesSet() {
106105
Assert.notNull(this.beanFactory, "BeanFactory must not be null");
107-
if (!this.beanFactory.containsBeanDefinition(IntegrationContextUtils.DISPOSABLES_BEAN_NAME)) {
108-
((BeanDefinitionRegistry) this.beanFactory)
109-
.registerBeanDefinition(IntegrationContextUtils.DISPOSABLES_BEAN_NAME,
110-
new RootBeanDefinition(Disposables.class, Disposables::new));
111-
}
112106
this.postProcessors.put(Filter.class, new FilterAnnotationPostProcessor(this.beanFactory));
113107
this.postProcessors.put(Router.class, new RouterAnnotationPostProcessor(this.beanFactory));
114108
this.postProcessors.put(Transformer.class, new TransformerAnnotationPostProcessor(this.beanFactory));
@@ -224,25 +218,28 @@ protected void processAnnotationTypeOnMethod(Object bean, String beanName, Metho
224218
}
225219
}
226220

221+
@SuppressWarnings("unchecked")
227222
private void postProcessMethodAndRegisterEndpointIfAny(Object bean, String beanName, Method method,
228223
Class<? extends Annotation> annotationType, List<Annotation> annotations,
229224
MethodAnnotationPostProcessor<?> postProcessor, Method targetMethod) {
230225

231226
Object result = postProcessor.postProcess(bean, beanName, targetMethod, annotations);
227+
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
228+
BeanDefinitionRegistry definitionRegistry = (BeanDefinitionRegistry) beanFactory;
232229
if (result instanceof AbstractEndpoint) {
233230
AbstractEndpoint endpoint = (AbstractEndpoint) result;
234231
String autoStartup = MessagingAnnotationUtils.resolveAttribute(annotations, "autoStartup",
235232
String.class);
236233
if (StringUtils.hasText(autoStartup)) {
237-
autoStartup = getBeanFactory().resolveEmbeddedValue(autoStartup);
234+
autoStartup = beanFactory.resolveEmbeddedValue(autoStartup);
238235
if (StringUtils.hasText(autoStartup)) {
239236
endpoint.setAutoStartup(Boolean.parseBoolean(autoStartup));
240237
}
241238
}
242239

243240
String phase = MessagingAnnotationUtils.resolveAttribute(annotations, "phase", String.class);
244241
if (StringUtils.hasText(phase)) {
245-
phase = getBeanFactory().resolveEmbeddedValue(phase);
242+
phase = beanFactory.resolveEmbeddedValue(phase);
246243
if (StringUtils.hasText(phase)) {
247244
endpoint.setPhase(Integer.parseInt(phase));
248245
}
@@ -255,8 +252,9 @@ private void postProcessMethodAndRegisterEndpointIfAny(Object bean, String beanN
255252

256253
String endpointBeanName = generateBeanName(beanName, method, annotationType);
257254
endpoint.setBeanName(endpointBeanName);
258-
getBeanFactory().registerSingleton(endpointBeanName, endpoint);
259-
getBeanFactory().initializeBean(endpoint, endpointBeanName);
255+
definitionRegistry.registerBeanDefinition(endpointBeanName,
256+
new RootBeanDefinition((Class<AbstractEndpoint>) endpoint.getClass(), () -> endpoint));
257+
beanFactory.getBean(endpointBeanName);
260258
}
261259
}
262260

spring-integration-core/src/test/java/org/springframework/integration/config/annotation/MessagingAnnotationPostProcessorChannelCreationTests.java

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)