最近有个需求,要做session的共享,于是用到了spring session以及redis。
1、对于redis的安装配置,请参考我的文章,这里不再累述:
http://blog.csdn.net/psy1100/article/details/51248274
2、要用到springsession,redis,需要依赖一下包:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
版本自己去选择,不一定要用我的这个版本哈。
3、需要在spring配置文件里添加如下配置:
spring session代理的配置:
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="1800"></property>
</bean>
redis的配置:
<bean id="zcJedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.url.write.url}" />
<property name="port" value="${redis.url.write.port}" />
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
<!--
<bean id="zcJedisConnectionFactoryQuery" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.url.read.url}" />
<property name="port" value="${redis.url.read.port}" />
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="${redis.pool.config.max.active}"/>
<property name="maxIdle" value="${redis.pool.config.max.idle}"/>
<property name="maxWait" value="${redis.pool.config.max.wait}"/>
</bean>
<bean id="zcRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="zcJedisConnectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
4、web.xml的配置:
<!-- session 代理 -->
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
ok,经过以上配置后,基本就可以使用spring session用redis做session共享了。
备注:这里不累述一些spring的其他的基本包,例如依赖注入的包等等。