JavaConfig配置redis


redis.properties

redis.url=192.168.200.128
redis.port=6379
redis.timeout=3000    
redis.password=123456   
redis.pool.maxTotal=200
redis.pool.maxIdle=20 
redis.pool.minIdle=5  
redis.pool.maxWait=3000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true

package cn.xxm.service.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;


import cn.xxm.pojo.product.Product;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {
	@Value("${redis.pool.maxTotal}")
    private Integer maxTotal;
	@Value("${redis.pool.maxIdle}")
	private Integer maxIdle;
	@Value("${redis.pool.maxWait}")
	private Integer maxWaitMillis;
	@Value("${redis.pool.testOnBorrow}")
	private Boolean testOnBorrow;
	@Value("${redis.url}")
	private String host;
	@Value("${redis.port}")
	private Integer port;
	
	/**
	 * JedisPoolConfig
	 */
	@Bean(name="jedisPoolConfig")
	public JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxTotal(maxTotal);
		jedisPoolConfig.setMaxIdle(maxIdle);
		jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);;
		jedisPoolConfig.setTestOnBorrow(testOnBorrow);
		return jedisPoolConfig;
	}


	/**
	 * JedisConnectionFactory
	 */
	@Bean(name="jedisConnectionFactory")
	public JedisConnectionFactory jedisConnectionFactory() {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setHostName(host);
		jedisConnectionFactory.setPort(port);
		jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
		
		return jedisConnectionFactory;
	}
	
	/**
	 * redisTemplate
	 */
//	@Bean(name="redisTemplate")
//	public StringRedisTemplate redisTemplate() {
//		StringRedisTemplate redisTemplate = new StringRedisTemplate();
//		redisTemplate.setConnectionFactory(jedisConnectionFactory());
//		return redisTemplate;
//	}
	
	@Bean  
    public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {  
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();  
        redisTemplate.setConnectionFactory(jedisConnectionFactory);  
  
        // 使用Jackson2JsonRedisSerialize 替换默认序列化  
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);  
  
        ObjectMapper objectMapper = new ObjectMapper();   
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);  
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);  
  
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);  
  
        // 设置String类型的序列化规则和 key的序列化规则  
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);  
        redisTemplate.setKeySerializer(new StringRedisSerializer());  
  
        // 设置Hash类型的序列化规则和 key的序列化规则 
        redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);  
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);  
  
        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);  
        redisTemplate.setEnableDefaultSerializer(true);  
        redisTemplate.afterPropertiesSet();  
        return redisTemplate;  
    } 
}

测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { RootConfig.class })
// @WebAppConfiguration
public class TestTestMy {

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	@Test
	public void testRedisTemple() throws Exception {
		// 测试String类型
		// ValueOperations<String, Object> value = redisTemplate.opsForValue();
		// System.err.println(value.get("username"));
		// System.err.println(value.get("age"));
		
		//测试Hash类型
		//具体调用
		 Map<String,String> map = new HashMap<String, String>();
		// map.put("value","code");
		// map.put("key","keyValue");
		// redisTemplate.opsForHash().putAll("hashOps",map);
		Map<Object, Object> entries = redisTemplate.opsForHash().entries("hashOps");
		for (Entry<Object, Object> obj : entries.entrySet()) {
			System.err.println(obj.getKey()+"---"+obj.getValue());
		}
	}
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值