Integrating Redis with Spring MVC Applications
Maven Dependency Configuration
Add the following dependencies to your project's pom.xml file. Ensure version compatibility to avoid errors.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
Check Maven repositories like https://mvnrepository.com/ for compatible versions.
Redis Environment Setup
Define Redis connection parameters in a properties file, such as redis-config.properties.
redis.host=127.0.0.1
redis.port=6379
redis.password=
redis.pool.maxIdle=20
redis.pool.minIdle=0
redis.pool.maxTotal=100
redis.pool.maxWait=-1
redis.pool.testOnBorrow=true
redis.database=1
redis.timeout=3000
Spring XML Configuration
Configure Spring beans in your application context file.
<context:property-placeholder location="classpath:redis-config.properties" ignore-unresolvable="true" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="minIdle" value="${redis.pool.minIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.password}" />
<property name="database" value="${redis.database}" />
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="timeout" value="${redis.timeout}" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="enableTransactionSupport" value="true" />
</bean>
Redis Utility Class Implemantation
Create a utility class to intercat with Redis.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class RedisService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void storeValue(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public Object retrieveValue(String key) {
return redisTemplate.opsForValue().get(key);
}
}
Note that with this configuration, stored value are limited to strings.