Search code examples
springspring-data-redis

Spring configuration for Radis, how the beans get wired?


In the following example from Getting started guide of Spring how the container bean gets connectionFactory? Does Spring Boot supplies a connectionFactory on its own?

Getting Started Messaging with Spring Redis

There are 5 beans :

  1. latch
  2. receiver
  3. listenerAdapter
  4. template
  5. container

latch gets created first. Then receiver because receiver constructor needs latch.Then listenerAdapter because it needs receiver.Both template and container need connectionFactory. In the code I do not find any method with name connectionFactory and annotated with @Bean.

@SpringBootApplication
    public class Application {
        @Bean
        RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                MessageListenerAdapter listenerAdapter) {

            RedisMessageListenerContainer container = new RedisMessageListenerContainer();
            container.setConnectionFactory(connectionFactory);
            container.addMessageListener(listenerAdapter, new PatternTopic("chat"));

            return container;
        }

        @Bean
        MessageListenerAdapter listenerAdapter(Receiver receiver) {
            return new MessageListenerAdapter(receiver, "receiveMessage");
        }

        @Bean
        Receiver receiver(CountDownLatch latch) {
            return new Receiver(latch);
        }

        @Bean
        CountDownLatch latch() {
            return new CountDownLatch(1);
        }

        @Bean
        StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
            return new StringRedisTemplate(connectionFactory);
        }
}

Solution

  • It's in the classpath of your project, this is what spring boot does