|
36 | 36 | import org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor; |
37 | 37 | import org.springframework.aop.support.NameMatchMethodPointcut; |
38 | 38 | import org.springframework.aop.support.NameMatchMethodPointcutAdvisor; |
39 | | -import org.springframework.beans.factory.DisposableBean; |
40 | 39 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
41 | 40 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 41 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
42 | 42 | import org.springframework.beans.factory.support.BeanDefinitionValidationException; |
| 43 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
43 | 44 | import org.springframework.context.annotation.Bean; |
44 | 45 | import org.springframework.core.GenericTypeResolver; |
45 | 46 | import org.springframework.core.ResolvableType; |
@@ -120,34 +121,27 @@ public abstract class AbstractMethodAnnotationPostProcessor<T extends Annotation |
120 | 121 |
|
121 | 122 | protected final ConfigurableListableBeanFactory beanFactory; // NOSONAR |
122 | 123 |
|
| 124 | + protected final BeanDefinitionRegistry definitionRegistry; // NOSONAR |
| 125 | + |
123 | 126 | protected final ConversionService conversionService; // NOSONAR |
124 | 127 |
|
125 | 128 | protected final DestinationResolver<MessageChannel> channelResolver; // NOSONAR |
126 | 129 |
|
127 | 130 | protected final Class<T> annotationType; // NOSONAR |
128 | 131 |
|
129 | | - protected final Disposables disposables; // NOSONAR |
130 | | - |
131 | 132 | @SuppressWarnings(UNCHECKED) |
132 | 133 | public AbstractMethodAnnotationPostProcessor(ConfigurableListableBeanFactory beanFactory) { |
133 | 134 | Assert.notNull(beanFactory, "'beanFactory' must not be null"); |
134 | 135 | this.messageHandlerAttributes.add(SEND_TIMEOUT_ATTRIBUTE); |
135 | 136 | this.beanFactory = beanFactory; |
| 137 | + this.definitionRegistry = (BeanDefinitionRegistry) beanFactory; |
136 | 138 | this.conversionService = this.beanFactory.getConversionService() != null |
137 | 139 | ? this.beanFactory.getConversionService() |
138 | 140 | : DefaultConversionService.getSharedInstance(); |
139 | 141 | this.channelResolver = ChannelResolverUtils.getChannelResolver(beanFactory); |
140 | 142 | this.annotationType = |
141 | 143 | (Class<T>) GenericTypeResolver.resolveTypeArgument(this.getClass(), |
142 | 144 | 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; |
151 | 145 | } |
152 | 146 |
|
153 | 147 | @Override |
@@ -187,23 +181,19 @@ public Object postProcess(Object bean, String beanName, Method method, List<Anno |
187 | 181 | } |
188 | 182 |
|
189 | 183 | private MessageHandler registerHandlerBean(String beanName, Method method, final MessageHandler handler) { |
190 | | - MessageHandler handlerBean = handler; |
191 | 184 | String handlerBeanName = generateHandlerBeanName(beanName, method); |
192 | | - if (handlerBean instanceof ReplyProducingMessageHandlerWrapper |
| 185 | + if (handler instanceof ReplyProducingMessageHandlerWrapper |
193 | 186 | && StringUtils.hasText(MessagingAnnotationUtils.endpointIdValue(method))) { |
194 | 187 | handlerBeanName = handlerBeanName + ".wrapper"; |
195 | 188 | } |
196 | | - if (handlerBean instanceof IntegrationObjectSupport) { |
197 | | - ((IntegrationObjectSupport) handlerBean).setComponentName( |
| 189 | + if (handler instanceof IntegrationObjectSupport) { |
| 190 | + ((IntegrationObjectSupport) handler).setComponentName( |
198 | 191 | handlerBeanName.substring(0, |
199 | 192 | handlerBeanName.indexOf(IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX))); |
200 | 193 | } |
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); |
207 | 197 | } |
208 | 198 |
|
209 | 199 | private void orderable(Method method, MessageHandler handler) { |
@@ -355,12 +345,9 @@ protected AbstractEndpoint createEndpoint(MessageHandler handler, @SuppressWarni |
355 | 345 | } |
356 | 346 | catch (DestinationResolutionException e) { |
357 | 347 | 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); |
364 | 351 | } |
365 | 352 | else { |
366 | 353 | throw e; |
|
0 commit comments