码迷,mamicode.com
首页 > 编程语言 > 详细

配置RedisTemplate、JedisPoolConfig、JedisConnectionFactory+自定义序列化 (java方式)

时间:2018-12-28 10:44:17      阅读:2941      评论:0      收藏:0      [点我收藏+]

标签:工程   default   work   serialize   nfa   host   template   key   schema   

java方式配置RedisTemplate

  //spring注入ben
   //@Bean(name = "redisTemplate")
    public RedisTemplate initRedisTemplate(){
    JedisPoolConfig poolConfig = new JedisPoolConfig();
        //最大空闲数
        poolConfig.setMaxIdle(50);
        //最大连接数
        poolConfig.setMaxTotal(100);
        //最大等待毫秒
        poolConfig.setMaxWaitMillis(20000);    

   //创建Jedis
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setHostName("localhost");
        connectionFactory.setPort(6379);
        //调用后初始化方法,没有它将抛出异常
        connectionFactory.afterPropertiesSet();
//自定义Redis序列化器
        RedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
        RedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //定义连接RedisTemplate,并设置连接工程
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(connectionFactory);
        //设置序列化器
        redisTemplate.setDefaultSerializer(stringRedisSerializer);
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);
        return redisTemplate;
    }

 xml方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 配置JedisPoolConfig-->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="50"/>
        <property name="maxTotal" value="100"/>
        <property name="maxWaitMillis" value="20000"/>
    </bean>
    <!--配置JedisConnectionFactory-->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="localhost"/>
        <property name="port" value="6379"/>
        <property name="poolConfig" ref="poolConfig"/>
    </bean>
    <!--使用字符串进行序列化-->
    <bean id="stringReadisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    <!--使用JDK的序列化器进行转化-->
    <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
    <!--配置Spring RedisTemplate-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="keySerializer" ref="stringReadisSerializer"/>
        <property name="valueSerializer" ref="stringReadisSerializer"/>
    </bean>
</beans>

 

配置RedisTemplate、JedisPoolConfig、JedisConnectionFactory+自定义序列化 (java方式)

标签:工程   default   work   serialize   nfa   host   template   key   schema   

原文地址:https://www.cnblogs.com/weibanggang/p/10188682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!