前提条件
1、已经安装成功了Redis server,并且正常使用。
2、创建基于maven的spring web工程。
本文主要讲解的是按照XML配置方法实现,另有注解方式可以实现,可参见官网。
官网对于一些依赖的最低要求:
If you are running in a Servlet Container (not required), Servlet 2.5+
If you are using other Spring libraries (not required), the minimum required version is Spring 3.2.14. While we re-run all unit tests against Spring 3.2.x, we recommend using the latest Spring 4.x version when possible.
@EnableRedisHttpSession requires Redis 2.8+.
于是我选择的版本如下
redis-3.2.1
spring 4.2.5.RELEASE
servlet 3.0.1
使用SPRING来共享SESSION基于REDIS按照以下步骤实现
1、添加POM文件依赖,主要的依赖有
2、在web.xml里面配置生成springSessionRepositoryFilter
这里面的参数大概介绍下
hostName:redis所在主机IP
port:redis所在主机监听的端口
password:redis上配置的认证auth的密码
3、在web.xml里面配置过滤器
扔个官网链接
http://docs.spring.io/spring-session/docs/current/reference/HTML5/guides/httpsession-xml.html
这个时候出现了一个问题,困扰了不少时间
严重: Exception starting filter springSessionRepositoryFilter
org.springframework.beans.factory. NoSuchBeanDefinitionException: No bean named 'springSessionRepositoryFilter' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1175)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1060)
at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)
at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:235)
at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:199)
报错springSessionRepositoryFilter不存在,经过几次和官网的例子对比已经网上找了很久答案依然无解。
最后赖着性子一句一句从官网介绍读取,可以看到这样一句话
上图中说明了是步骤1创建的springSessionRepositoryFilter。仔细检查web.xml。确保springSessionRepositoryFilter先于启动flter创建了即可。
DEMO代码下载链接
http://download.csdn.NET/detail/tgj1202/9583951
原文地址:http://blog.csdn.net/tgj1202/article/details/51995853