SpringSessionRedis配置及发现的问题

最近写项目,需要把session放入Redis中,来实现分布式。我本来要用Tomcat部署Redis这种方法,但是依赖于容器了。无意中发现了SpringSession,这可挺不错的,写完了发现不好用,问度娘也没弄明白,最后我写了2个demo一个springMVC的,一个spring整合struts2的,发现SpringSession需要SpringMVC的支持。也就是说我的项目用不了了。

先说说springsession的配置吧:

一、Maven中pom.xml文件中添加(选一种添加上就行):

[html]  view plain  copy
  1. <span style="white-space:pre">        </span><!--1、redis-整合-->  
  2.         <dependency>  
  3.             <groupId>org.springframework.session</groupId>  
  4.             <artifactId>spring-session-data-redis</artifactId>  
  5.             <version>1.0.2.RELEASE</version>  
  6.         </dependency>  
  7.   
  8.         <!-- 2、Redis -->  
  9.         <dependency>  
  10.             <groupId>org.springframework.data</groupId>  
  11.             <artifactId>spring-data-redis</artifactId>  
  12.             <version>1.4.2.RELEASE</version>  
  13.         </dependency>        
  14.         <dependency>  
  15.             <groupId>redis.clients</groupId>  
  16.             <artifactId>jedis</artifactId>  
  17.             <version>2.5.2</version>  
  18.         </dependency>  
  19.         <dependency>  
  20.                <groupId>org.springframework.session</groupId>  
  21.                <artifactId>spring-session</artifactId>  
  22.                <version>1.0.2.RELEASE</version>  
  23.         </dependency>  
  24.         <dependency>  
  25.               <groupId>org.apache.commons</groupId>  
  26.               <artifactId>commons-pool2</artifactId>  
  27.               <version>2.2</version>  
  28.         </dependency>  
二、在spring配置文件(applicationContext.xml)中添加代码:

[html]  view plain  copy
  1. <span style="white-space:pre">    </span><!-- 自动扫描 -->  
  2.      <context:annotation-config/>  
  3.     <!-- 配置spring-session -->   
  4.     <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">   
  5.         <!-- 过期时间100分钟 -->  
  6.         <property name="maxInactiveIntervalInSeconds" value="6000"></property>  
  7.     </bean>    
  8.     <!-- redis连接池 -->  
  9.     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" />   
  10.           
  11.     <bean  id="jedisConnectionFactory"  class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  >    
  12.         <property  name="hostName"  value="10.4.120.180" />    
  13.         <property  name="port"  value="6379" />    
  14.         <property  name="poolConfig"  ref="jedisPoolConfig" />    
  15.     </bean>    
三、在web.xml中添加过滤即可:

[html]  view plain  copy
  1. <span style="white-space:pre">    </span><!-- Spring Session的Filter -->  
  2.     <filter>  
  3.         <filter-name>springSessionRepositoryFilter</filter-name>  
  4.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  5.     </filter>  
  6.     <filter-mapping>  
  7.         <filter-name>springSessionRepositoryFilter</filter-name>  
  8.         <url-pattern>/*</url-pattern>  
  9.     </filter-mapping>  
  10.     <session-config>  
  11.         <session-timeout>30</session-timeout>  
  12.     </session-config>   
这样就自动将session放入到reids库中了。

补充:

Spring的版本为4.1.6以上

javax.servlet-api需要3.0.1版本以上

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值