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

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

二. 解决办法

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

完善的代码如下:

@Configuration@EnableRedisHttpSessionpublic class HttpSessionConfig {@Autowiredprivate FindByIndexNameSessionRepository<? extends Session> sessionRepository;​/*** SpringSessionBackedSessionRegistry是Session为Spring Security提供的用于在集群环境中控制并发会话的注册表实现类*/@Beanpublic 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的集群管理。

RedisConnectionFactory is required异常解决相关推荐

  1. orcal 数据库 maven架构 ssh框架 的全xml环境模版 及常见异常解决

    创建maven项目后,毫不犹豫,超简单傻瓜式搞定dependencies(pom.xml 就是maven的依赖管理),这样你就有了所有你要的包 <project xmlns="http ...

  2. SpringBoot集成Redis报non null key required(已解决)

    SpringBoot集成Redis报non null key required(已解决) 注意是不是给的key值或传过来的值是不是空的,如果是空的就会报这个错 接下来将流程走下去: 1.创建Sprin ...

  3. Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 异常解决

    Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration required a bean of type 'xxx. ...

  4. JDK8 stream toMap() java.lang.IllegalStateException: Duplicate key异常解决(key重复)

    测试又报bug啦 接到测试小伙伴的问题,说是一个接口不返回数据了,好吧,虽然不是我写的接口任务落到头上也得解决,本地调试了一下,好家伙,直接抛了个异常出来,这又是哪位大哥喝醉了写的代码... Exce ...

  5. WEB站点使用企业库安全性异常解决办法

    WEB站点使用企业库安全性异常解决办法 我们在WEB站点使用企业库的时候,有时候会有如下的异常产生: 安全性异常     说明: 应用程序试图执行安全策略不允许的操作.要授予此应用程序所需的权限,请与 ...

  6. 记录一次cefsharp1输入法在win7下异常解决定位

    最近几天都被基于cefSharp封装的浏览器控件搞疯了!对于cefSharp基本满足当前所做项目的需求,但是有一个问题一直困扰我,那就是系统中偶尔会出现输入法不能转换到中文.而且这个问题似乎没有什么规 ...

  7. mysql出现1499错误_连接MySQL时出现1449与1045异常解决办法

    连接MySQL时出现1449与1045异常解决办法 mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解 ...

  8. [异常解决] ubuntu上安装虚拟机遇到的问题(vmware坑了,virtual-box简单安装,在virtual-box中安装精简版win7)

    [异常解决] ubuntu上安装虚拟机遇到的问题(vmware坑了,virtual-box简单安装,在virtual-box中安装精简版win7) 参考文章: (1)[异常解决] ubuntu上安装虚 ...

  9. variable ‘‘ of type ‘‘ referenced from scope ‘‘, but it is not defined 异常解决方法

    variable '' of type '' referenced from scope '', but it is not defined 异常解决方法 参考文章: (1)variable '' o ...

  10. [M]MagicTable转换异常解决方法

    [M]MagicTable转换异常解决方法 参考文章: (1)[M]MagicTable转换异常解决方法 (2)https://www.cnblogs.com/yiyun/p/5259384.html ...

最新文章

  1. Hibernate面试题
  2. mysql数据库技术方案,MySql数据库优化方案
  3. Android MediaRecorder系统结构
  4. 文件上传案例——客户端和服务端套接字
  5. PL/SQL Developer跑在Oracle 64位数据库上初始化错误
  6. SpringAOP xml 方式和注解简单实现日志处理
  7. Java编程开发中高效编码的7个技巧?你应该知道……
  8. CCF 201412-1 门禁系统
  9. 基于CSE的微服务架构实践-Spring Cloud技术栈选型
  10. Recurrent Neural Networks
  11. 非对称加密(1)非对称加密原理
  12. VLAN专题之三:VLAN的访问链接
  13. JS中动态创建元素的三种方法
  14. 前缀树python实现
  15. 秋天的第一杯奶茶,没喝到?那这个你绝对不能再错过
  16. 用Python绘制一个简单的奥运五环
  17. 皓月酒店管理系统: Jquery + Servlet + Mybatis
  18. 奥鹏 大工21秋《计算机网络技术》在线作业
  19. 51单片机频率计c语言程序,51单片机简易频率计源代码
  20. 微信公众号 二维码场景统计 场景二维码

热门文章

  1. CSS:两种方式画一个梯形
  2. Vulhub-DC-8靶场实战攻略
  3. insert用法小结
  4. .NET Core剪裁器背后的技术
  5. 51jiqiren小语种语音机器人
  6. delphi math 单元函数
  7. c语言json使用,cJSON使用(二)
  8. 打开chm手册显示已取消到该网页的导航
  9. 手把手教你写 Word 版本PRD
  10. 第三章:Servlet基础