实现Spring-session要注意的点

1.导入jar包

 2.在web.xml 添加过滤器,放在最前面

    <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>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

3.新建一个spring-session.xml

因为redis我配的是硝兵模式,你可以换成自己的redis,这里主要是RedisHttpSessionConfiguration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:p="http://www.springframework.org/schema/p"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"
		  p:maxIdle="300" p:maxWaitMillis="1000" p:testOnBorrow="true">
	</bean>
	<!-- 添加RedisHttpSessionConfiguration用于session共享 -->
	<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="10" />
		<property name="maxIdle" value="100" />
		<property name="maxWaitMillis" value="3000" />
		<property name="testOnBorrow" value="true" />
	</bean>
	<bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
		<property name="master">
			<bean class="org.springframework.data.redis.connection.RedisNode">
				<!--这个值要和Sentinel中指定的master的值一致,不然启动时找不到Sentinel会报错的-->
				<property name="name" value="lhredismaster"></property>
			</bean>
		</property>
		<!--指定Sentinel的IP和端口,不是Master和Slave的-->
		<property name="sentinels">
			<set>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value=""></constructor-arg>
					<constructor-arg name="port" value="26379"></constructor-arg>
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value=""></constructor-arg>
					<constructor-arg name="port" value="26380"></constructor-arg>
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value=""></constructor-arg>
					<constructor-arg name="port" value="26381"></constructor-arg>
				</bean>
			</set>
		</property>
	</bean>
	<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:password="zfxxlhredis">
		<constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>
		<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
	</bean>

	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="redisConnectionFactory"></property>
		<property name="keySerializer">
			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
		<property name="valueSerializer">
			<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
		</property>
	</bean>


</beans>

 4.新建一个applicationContext.xml

<bean id="propertyConfigurerRedis"
		  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="order" value="1" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="systemPropertiesMode" value="1" />
		<property name="searchSystemEnvironment" value="true" />
		<property name="locations">
			<list>
				<value>classpath:config.properties</value>
			</list>
		</property>
	</bean>

5.使两个新加的文件生效

在beans.xml中

	<import resource="applicationContext.xml" />
	<import resource="spring-session.xml" />

--------------------------------------------------------------------------------------------------

Guess you like

Origin blog.csdn.net/qq_35204957/article/details/120749498