1

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.

5
  • Did you "refresh" the application context after registering your bean? Commented May 24, 2017 at 12:24
  • Can you show SimpleMessageListenerContainer class Commented May 24, 2017 at 12:24
  • @pvpkiran: Here is the class docs.spring.io/spring-amqp/docs/current/api/org/springframework/… Commented May 24, 2017 at 12:25
  • @AndrewWhite, I tried but got an exception 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? Commented May 24, 2017 at 12:31
  • @stefku thank you man!!. I was having same issue for dynamically created factory beans!!! Commented Dec 20, 2024 at 3:06

1 Answer 1

3
beanFactory.autowireBean(container); beanFactory.initializeBean(container, name); 

Add this before the line

beanFactory.registerSingleton(name, container); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. this worked like a charm.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.