Spring使用RedisTemplate的xml配置

1,spring-redis.xml

<!-- redis连接池的配置 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="500" />
		<property name="maxIdle" value="50"/>
		<property name="minIdle" value="20"/>
		<property name="maxWaitMillis" value="5000" />
		<property name="testWhileIdle" value="true"/>
		<property name="minEvictableIdleTimeMillis" value="240000"></property>
		<property name="numTestsPerEvictionRun" value="3"></property>
		<property name="timeBetweenEvictionRunsMillis" value="30000"></property>
	</bean>

	<!-- 工厂类配置 -->
	<bean id="jedisConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="${redis.ip}" />
		<property name="port" value="${redis.port}" />
		<property name="password" value="${redis.password}" />
		<property name="poolConfig" ref="jedisPoolConfig" />
		<property name="timeout" value="15000"></property>
		<property name="usePool" value="true"></property>
	</bean>

<!-- redisTemplate配置 -->
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory"   ref="jedisConnectionFactory" />
		<property name="keySerializer">
			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
		<property name="valueSerializer">
			<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
		</property>
		<property name="hashKeySerializer">
			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
		</property>
		<property name="hashValueSerializer">
			<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
		</property>
	</bean>

2,redis.properties

redis.ip=172.0.0.1
redis.port=8089
redis.password=123456

 

要让RedisTemplate在mvc XML中正常使用,你需要进行如下配置: 1.首先,确保你已经在Maven或Gradle中引入了spring-data-redis的依赖。如果你使用的是Maven,可以在pom.xml中添加如下依赖: ```xml <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.5.5</version> </dependency> ``` 如果你使用的是Gradle,可以在build.gradle中添加如下依赖: ```groovy implementation 'org.springframework.data:spring-data-redis:2.5.5' ``` 2.在mvc XML配置Redis连接工厂: ```xml <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="1000"/> <property name="maxIdle" value="10"/> <property name="minIdle" value="5"/> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="localhost"/> <property name="port" value="6379"/> <property name="poolConfig" ref="jedisPoolConfig"/> </bean> ``` 3.配置RedisTemplate: ```xml <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> </bean> ``` 这里我们使用了StringRedisSerializer来进行序列化,如果你需要使用其他类型的序列化器,可以自行替换。现在,你就可以在mvc XML中愉快地使用RedisTemplate了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值