RedisConnectionFactory is required异常解决

java.lang.IllegalStateException: RedisConnectionFactory is required异常解决

 

一. 异常现象

我在SpringBoot中实现集群会话管理时,在对Spring Session与Redis进行整合的过程中,出现了如下异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionRepository' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.session.data.redis.RedisIndexedSessionRepository]: Circular reference involving containing bean 'org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration' - consider declaring the factory method as static for independence from its containing instance. Factory method 'sessionRepository' threw exception; nested exception is java.lang.IllegalStateException: RedisConnectionFactory is required at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE] at ..... Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.session.data.redis.RedisIndexedSessionRepository]: Circular reference involving containing bean 'org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration' - consider declaring the factory method as static for independence from its containing instance. Factory method 'sessionRepository' threw exception; ... 114 common frames omitted

Caused by: java.lang.IllegalStateException: RedisConnectionFactory is required at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.data.redis.core.RedisAccessor.afterPropertiesSet(RedisAccessor.java:38) ~[spring-data-redis-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.data.redis.core.RedisTemplate.afterPropertiesSet(RedisTemplate.java:127) ~[spring-data-redis-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration.createRedisTemplate(RedisHttpSessionConfiguration.java:291) ~[spring-session-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration.sessionRepository(RedisHttpSessionConfiguration.java:120) ~[spring-session-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_192] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_192] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_192] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_192] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE] ... 115 common frames omitted

最直接的异常就是 java.lang.IllegalStateException: RedisConnectionFactory is required

根据提示可以看到,就是Redis连接工厂没有设置好,导致程序无法启动起来。

下面是我的原始代码: 

@Configuration
 @EnableRedisHttpSession
 public class HttpSessionConfig {
 ​
     /**
      * 创建Redis连接,默认是连接本地localhost:6379
      */
     @Bean
     public RedisConnectionFactory connectionFactory(){
         return new JedisConnectionFactory();
     }
 ​
     /**
      * HttpSession的事件监听,改用Session提供的会话注册表.
      */
     @Bean
     public HttpSessionEventPublisher httpSessionEventPublisher(){
 ​
         return new HttpSessionEventPublisher();
     }
 ​
 ​
     @Autowired
     private FindByIndexNameSessionRepository<? extends Session> sessionRepository;
 ​
     /**
      * SpringSessionBackedSessionRegistry是Session为Spring Security提供的用于在集群环境中控制并发会话的注册表实现类
      */
     @Bean
     public SpringSessionBackedSessionRegistry sessionRegistry(){
 ​
         return new SpringSessionBackedSessionRegistry<>(sessionRepository);
     }    
     
 }

从代码中可以看到,因为我要连接redis,所以是写了RedisConnectionFactory配置代码的!

二. 解决办法

后来经过分析,实际上在SpringSessionBackedSessionRegistry创建过程中,是不需要我们自己设置这个RedisConnectionFactory和HttpSessionEventPublisher代码的,所以解决办法也很简单,就是去掉RedisConnectionFactory和HttpSessionEventPublisher相关代码。

完善的代码如下:

@Configuration
 @EnableRedisHttpSession
 public class HttpSessionConfig {
     
     @Autowired
     private FindByIndexNameSessionRepository<? extends Session> sessionRepository;
 ​
     /**
      * SpringSessionBackedSessionRegistry是Session为Spring Security提供的用于在集群环境中控制并发会话的注册表实现类
      */
     @Bean
     public SpringSessionBackedSessionRegistry sessionRegistry(){
 ​
         return new SpringSessionBackedSessionRegistry<>(sessionRepository);
     }
 ​
     //解决Caused by: java.lang.IllegalStateException: RedisConnectionFactory is required异常:,必须移除如下代码!!!
     /**
      * 创建Redis连接,默认是连接本地localhost:6379
      */
 //    @Bean
 //    public RedisConnectionFactory connectionFactory(){
 //        return new JedisConnectionFactory();
 //    }
 ​
     /**
      * HttpSession的事件监听,改用Session提供的会话注册表.
      */
 //    @Bean
 //    public HttpSessionEventPublisher httpSessionEventPublisher(){
 //
 //        return new HttpSessionEventPublisher();
 //    }
     
 }

最后就实现了Session的集群管理。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一一哥Sun

您的鼓励是我继续创作的动力哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值