1.引入依赖(pom文件)
1 <dependency> 2 <groupId>org.springframework.data</groupId> 3 <artifactId>spring-data-redis</artifactId> 4 <version>2.1.4.RELEASE</version> 5 </dependency> 6 <dependency> 7 <groupId>redis.clients</groupId> 8 <artifactId>jedis</artifactId> 9 <version>2.9.0</version> 10 </dependency>
2.redis配置(redis.properties)
redis.host=host
redis.port=6379
redis.password=""
redis.maxIdle=400
redis.maxTotal=6000
redis.maxWaitMillis=1000
redis.blockWhenExhausted=true
redis.testOnBorrow=true
redis.db=1
3.bean配置
方法一:springConfig.xml
1 <!-- 加载redis配置 --> 2 <context:property-placeholder location="classpath:properties/redis.properties" /> 3 4 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 5 <!-- 最大空闲数 --> 6 <property name="maxIdle" value="${redis.maxIdle}" /> 7 <!-- 最大空连接数 --> 8 <property name="maxTotal" value="${redis.maxTotal}" /> 9 <!-- 最大等待时间 --> 10 <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> 11 <!-- 连接超时时是否阻塞,false时报异常,true阻塞直到超时, 默认true --> 12 <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}" /> 13 <!-- 返回连接时,检测连接是否成功 --> 14 <property name="testOnBorrow" value="${redis.testOnBorrow}" /> 15 </bean> 16 17 <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 18 p:host-name="${redis.host}" 19 p:port="${redis.port}" 20 p:password="${redis.password}" 21 p:database="${redis.db}" 22 p:pool-config-ref="poolConfig"/> 23 24 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> 25 <property name="connectionFactory" ref="connectionFactory" /> 26 <property name="keySerializer"> 27 <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> 28 </property> 29 <property name="valueSerializer"> 30 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> 31 </property> 32 <property name="hashKeySerializer"> 33 <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> 34 </property> 35 <property name="hashValueSerializer"> 36 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> 37 </property> 38 <!--开启事务 --> 39 <property name="enableTransactionSupport" value="true"></property> 40 </bean> 41 42 <!--自定义redis工具类,在需要缓存的地方注入此类 --> 43 <bean id="redisService" class="services.RedisService"> 44 <property name="redisTemplate" ref="redisTemplate" /> 45 </bean> 46 47 <!--启用spring的一些annotation --> 48 <context:annotation-config/>
方法二:RedisConfig.java
1 package configs; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.data.redis.connection.RedisStandaloneConfiguration; 7 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 8 import org.springframework.data.redis.core.RedisTemplate; 9 import org.springframework.data.redis.serializer.StringRedisSerializer; 10 import redis.clients.jedis.JedisPoolConfig; 11 import services.RedisService; 12 13 @Configuration 14 public class RedisConfig { 15 16 @Value("${redis.host}") 17 private String hostName; 18 19 @Value("${redis.port}") 20 private int port; 21 22 @Value("${redis.password}") 23 private String password; 24 25 @Value("${redis.db}") 26 private int database; 27 28 29 /** 30 * redis链接池配置 31 * @return JedisPoolConfig 32 */ 33 @Bean 34 public JedisPoolConfig jedisPoolConfig(){ 35 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 36 37 jedisPoolConfig.setMaxTotal(100); 38 39 jedisPoolConfig.setMinIdle(20); 40 41 jedisPoolConfig.setMaxWaitMillis(10000); 42 43 return jedisPoolConfig; 44 } 45 46 /** 47 * redis工厂方法 48 * @return JedisConnectionFactory 49 */ 50 @Bean 51 public JedisConnectionFactory jedisConnectionFactory() { 52 System.out.println(hostName); 53 54 RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(hostName, port); 55 56 redisStandaloneConfiguration.setDatabase(database); 57 58 redisStandaloneConfiguration.setPassword(password); 59 60 return new JedisConnectionFactory(redisStandaloneConfiguration); 61 } 62 63 @Bean 64 public RedisTemplate redisTemplate(){ 65 RedisTemplate redisTemplate = new RedisTemplate(); 66 67 redisTemplate.setConnectionFactory(jedisConnectionFactory()); 68 69 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); 70 71 redisTemplate.setDefaultSerializer(stringRedisSerializer); 72 73 redisTemplate.setEnableTransactionSupport(true); 74 75 return redisTemplate; 76 } 77 78 @Bean 79 public RedisService redisService(){ 80 RedisService redisService = new RedisService(); 81 82 redisService.setRedisTemplate(redisTemplate()); 83 84 return redisService; 85 } 86 }
4.使用
1 @Controller 2 public class HelloController { 3 4 @Resource 5 private RedisService redisService; 6 7 @RequestMapping("hello") 8 @ResponseBody 9 public Movies hello(HttpServletRequest request){ 10 redisService.getKey(); 11 12 Movies movies = new Movies(); 13 return movies; 14 } 15 }