I would like to register a spring bean programatically (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) with
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(queueName); container.setMessageListener(listenerAdapter); beanFactory.registerSingleton(name, container); the containing class implements InitializingBean where beanFactory gets injected via
@Autowired private void setBeanFactory(ConfigurableListableBeanFactory beanFactory) { this.beanFactory = beanFactory; } The problem is, that the resulting bean does not work properly. Debugging showed that for example the ApplicationEventPublisher was not set on SimpleMessageListenerContainer which implements ApplicationEventPublisherAware.
In turn, when I register the bean via @Bean annotation it works properly:
@Bean public SimpleMessageListenerContainer zuteilungsProviderContainer( @SuppressWarnings("SpringJavaAutowiringInspection") ConnectionFactory connectionFactory, MessageListenerAdapter tourZuteilungenListenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(Q_BRIEFING_WINDOW); container.setMessageListener(tourZuteilungenListenerAdapter); return container; } When creating the bean programatically, for some reason the wiring or post processing does not work properly. I debugged a lot but I got lost in spring details.
What do I miss? I would be very grateful for any hint.
GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once. Apparently, the context had already refreshed (not by my though). Do I maybe have a problem with order of registering the beans?