标签:工程 default work serialize nfa host template key schema
//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 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