|
51 | 51 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
52 | 52 | import org.springframework.beans.factory.ObjectFactory; |
53 | 53 | import org.springframework.beans.factory.SmartInitializingSingleton; |
54 | | -import org.springframework.beans.factory.config.AutowireCapableBeanFactory; |
55 | 54 | import org.springframework.beans.factory.config.BeanExpressionContext; |
56 | 55 | import org.springframework.beans.factory.config.BeanExpressionResolver; |
57 | 56 | import org.springframework.beans.factory.config.BeanPostProcessor; |
58 | 57 | import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
59 | 58 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
60 | 59 | import org.springframework.beans.factory.config.Scope; |
| 60 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 61 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
61 | 62 | import org.springframework.context.ApplicationContext; |
62 | 63 | import org.springframework.context.ApplicationContextAware; |
63 | 64 | import org.springframework.context.ConfigurableApplicationContext; |
|
84 | 85 | import org.springframework.kafka.retrytopic.RetryTopicBootstrapper; |
85 | 86 | import org.springframework.kafka.retrytopic.RetryTopicConfiguration; |
86 | 87 | import org.springframework.kafka.retrytopic.RetryTopicConfigurer; |
| 88 | +import org.springframework.kafka.retrytopic.RetryTopicInternalBeanNames; |
87 | 89 | import org.springframework.kafka.support.KafkaNull; |
88 | 90 | import org.springframework.kafka.support.TopicPartitionOffset; |
89 | 91 | import org.springframework.lang.Nullable; |
@@ -481,19 +483,25 @@ private boolean processMainAndRetryListeners(KafkaListener kafkaListener, Object |
481 | 483 | } |
482 | 484 |
|
483 | 485 | private RetryTopicConfigurer getRetryTopicConfigurer() { |
484 | | - try { |
485 | | - return this.beanFactory.getBean(RetryTopicConfigurer.class); |
486 | | - } |
487 | | - catch (NoSuchBeanDefinitionException e) { |
488 | | - if (!(this.beanFactory instanceof AutowireCapableBeanFactory)) { |
489 | | - throw new IllegalStateException("BeanFactory must be an instance of " |
490 | | - + AutowireCapableBeanFactory.class.getSimpleName() |
491 | | - + " Provided beanFactory: " + this.beanFactory.getClass().getSimpleName(), e); |
492 | | - } |
493 | | - ((AutowireCapableBeanFactory) this.beanFactory) |
494 | | - .createBean(RetryTopicBootstrapper.class) |
495 | | - .bootstrapRetryTopic(); |
496 | | - return this.beanFactory.getBean(RetryTopicConfigurer.class); |
| 486 | + bootstrapRetryTopicIfNecessary(); |
| 487 | + return this.beanFactory.getBean(RetryTopicInternalBeanNames.RETRY_TOPIC_CONFIGURER, RetryTopicConfigurer.class); |
| 488 | + } |
| 489 | + |
| 490 | + private void bootstrapRetryTopicIfNecessary() { |
| 491 | + if (!(this.beanFactory instanceof BeanDefinitionRegistry)) { |
| 492 | + throw new IllegalStateException("BeanFactory must be an instance of " |
| 493 | + + BeanDefinitionRegistry.class.getSimpleName() |
| 494 | + + " to bootstrap the RetryTopic functionality. Provided beanFactory: " |
| 495 | + + this.beanFactory.getClass().getSimpleName()); |
| 496 | + } |
| 497 | + BeanDefinitionRegistry registry = (BeanDefinitionRegistry) this.beanFactory; |
| 498 | + if (!registry.containsBeanDefinition(RetryTopicInternalBeanNames |
| 499 | + .RETRY_TOPIC_BOOTSTRAPPER)) { |
| 500 | + registry.registerBeanDefinition(RetryTopicInternalBeanNames |
| 501 | + .RETRY_TOPIC_BOOTSTRAPPER, |
| 502 | + new RootBeanDefinition(RetryTopicBootstrapper.class)); |
| 503 | + this.beanFactory.getBean(RetryTopicInternalBeanNames |
| 504 | + .RETRY_TOPIC_BOOTSTRAPPER, RetryTopicBootstrapper.class).bootstrapRetryTopic(); |
497 | 505 | } |
498 | 506 | } |
499 | 507 |
|
|
0 commit comments