SpringSessionRedis配置及发现的问题

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

先说说springsession的配置吧:

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

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

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

[html] view plain copy
  1. <spanstyle="white-space:pre"></span><!--SpringSession的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版本以上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值