Radis的Spring配置,如何连接bean?

时间:2017-04-15 06:32:18

标签: spring spring-data-redis

在Spring的入门指南中的以下示例中,容器bean如何获​​取connectionFactory? Spring Boot是否自己提供了connectionFactory?

Getting Started Messaging with Spring Redis

有5个豆子:

  1. 闩锁
  2. 接收器
  3. listenerAdapter
  4. 模板
  5. 容器
  6. 首先创建

    latch。然后接收器因为接收器构造函数需要latch.Then listenerAdapter因为它需要receiver.Both模板和容器需要connectionFactory。 在代码中,我找不到任何名为connectionFactory且使用@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);
            }
    }
    

1 个答案:

答案 0 :(得分:0)

它位于项目的类路径中,这就是spring boot的作用