spring-session-redis存储(转载)

 前提条件

     1、已经安装成功了Redis server,并且正常使用。

     2、创建基于maven的spring web工程。

     本文主要讲解的是按照XML配置方法实现,另有注解方式可以实现,可参见官网。

 

     官网对于一些依赖的最低要求:

Java 5+

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+.

 

    于是我选择的版本如下

JDK1.8
redis-3.2.1
spring 4.2.5.RELEASE
servlet 3.0.1

 

    使用SPRING来共享SESSION基于REDIS按照以下步骤实现

    1、添加POM文件依赖,主要的依赖有

 

[xml]  view plain  copy
 
  1. <dependency>  
  2.           <groupId>org.springframework.session</groupId>  
  3.           <artifactId>spring-session-data-redis</artifactId>  
  4.           <version>1.3.0.RELEASE</version>  
  5. </dependency>  
  6. <dependency>  
  7.           <groupId>redis.clients</groupId>  
  8.           <artifactId>jedis</artifactId>  
  9.           <version>2.8.1</version>  
  10. </dependency>  
  11. <dependency>  
  12.         <groupId>org.springframework</groupId>  
  13.         <artifactId>spring-web</artifactId>  
  14.         <version>4.2.5.RELEASE</version>  
  15. </dependency>  

 2、在web.xml里面配置生成springSessionRepositoryFilter

 

 

[xml]  view plain  copy
 
  1. <context:annotation-config/>  
  2.     <bean id="redisHttpSessionConfiguration"  
  3.           class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">  
  4.         <property name="maxInactiveIntervalInSeconds" value="600"/>  
  5.     </bean>  
  6.   
  7.     <bean id="jedisConnectionFactory"  
  8.           class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">  
  9.         <property name="hostName" value="192.0.0.1"/>  
  10.         <property name="port" value="19900"/>  
  11.         <property name="password" value="password" />  
  12.         <property name="timeout" value="3000"/>  
  13.         <property name="usePool" value="true"/>  
  14.         <property name="poolConfig" ref="jedisPoolConfig"/>  
  15.     </bean>  
  16.   
  17.     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  18.         <property name="connectionFactory" ref="jedisConnectionFactory"/>  
  19.     </bean>  

   这里面的参数大概介绍下

 

   hostName:redis所在主机IP

   port:redis所在主机监听的端口

   password:redis上配置的认证auth的密码

 

3、在web.xml里面配置过滤器

 

[xml]  view plain  copy
 
  1. <filter>  
  2.     <filter-name>springSessionRepositoryFilter</filter-name>  
  3.     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  4.   </filter>  
  5.   <filter-mapping>  
  6.     <filter-name>springSessionRepositoryFilter</filter-name>  
  7.     <url-pattern>/*</url-pattern>  
  8.   </filter-mapping>  

 

扔个官网链接

http://docs.spring.io/spring-session/docs/current/reference/HTML5/guides/httpsession-xml.html

 

 

   这个时候出现了一个问题,困扰了不少时间

七月 22, 2016 3:35:37 下午 org.apache.catalina.core.StandardContext filterStart
严重: 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

猜你喜欢

转载自wb284551926.iteye.com/blog/2358530