Spring整合Redis之ConnectionFactory从何而来?_Java_hsx201612的博客-CSDN博客 ---- 有用 林进雨

最近在学习Redis的时候,入门的时候看了个demo

 @Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory){         RedisTemplate<String, Object> template = new RedisTemplate<>();        template.setConnectionFactory(connectionFactory);}

很是奇怪,这个connectionFactory从哪里来的?

于是乎,我发现了RedisAutoConfiguration这个类,但是答案不在这里,通过继续深入到了

LettuceConnectionConfiguration这个配置类中去,

@Bean@ConditionalOnMissingBean(RedisConnectionFactory.class)public LettuceConnectionFactory redisConnectionFactory(ClientResources clientResources)            throws UnknownHostException {        LettuceClientConfiguration clientConfig = getLettuceClientConfiguration(clientResources,                this.properties.getLettuce().getPool());        return createLettuceConnectionFactory(clientConfig);    }

这下问题解决了,在这里已经对connctionFactory进行了转配,所以demo中传参的时候,spring就会将这个bean注入。

通过debug,也可以发现,demo中的那个连接工厂就是

LettuceConnectionFactory的实例。


Original url: Access
Created at: 2020-04-16 17:03:20
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论