spring3 redis如何配置
时间: 2023-07-05 21:31:03 浏览: 54
在Spring 3中,可以使用Spring的Redis模块来实现对Redis的操作。以下是配置Redis的步骤:
1. 添加Redis依赖
在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.4.1</version>
</dependency>
```
2. 配置连接工厂
在Spring的配置文件中,配置Redis连接工厂:
```
<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
</bean>
```
3. 配置Redis模板
在Spring的配置文件中,配置Redis模板:
```
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"/>
<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>
```
4. 配置Redis缓存管理器
在Spring的配置文件中,配置Redis缓存管理器:
```
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg ref="redisTemplate"/>
</bean>
```
这样就完成了Spring 3中Redis的配置。在代码中使用Redis缓存时,可以通过注入Redis模板和Redis缓存管理器来实现对Redis的操作。