SpringSessionRedis配置及发现的问题

来源:互联网 发布:网络派出所电话 编辑:程序博客网 时间:2024/06/09 19:31

最近写项目,需要把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版本以上

0 0
原创粉丝点击