When spring and Redis are combined, the configuration bean is often the first to fix the Jedispoolconfig object, which reads:
<bean id= "Poolconfig" class= "Redis.clients.jedis.JedisPoolConfig" >
<property name= "maxactive" value= "/>"
<property name= "Maxidle" value= "/>"
<property name= "maxwait" value= "20000"/>
</bean>
Then configure the Jedisconnectionfactory object:
<bean id= "ConnectionFactory" class= " Org.springframework.data.redis.connection.jedis.JedisConnectionFactory,
<property name= "HostName" Value= "192.168.36.131"/>
<property name= "Port" value= "6379"/>
<property name= "password" value= " 123456 "/>
<property name=" Poolconfig "ref=" Poolconfig "/>
</bean>
Because Java objects cannot be stored directly in Redis, you need to serialize Java objects, have a key sequencer and a value sequencer, and configure key and value in the Redistemplate object:
<bean id= "Jdkserializationredisserializer" class= " Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer "/>
<bean id=" Stringredisserializer "class=" Org.springframework.data.redis.serializer.StringRedisSerializer "/>
<bean Id= "Redistemplate" class= "org.springframework.data.redis.core.RedisTemplate";
<property name= " ConnectionFactory "ref=" ConnectionFactory "/>
<property name=" Keyserializer "ref=" StringRedisSerializer "/
<property name= "ValueSerializer" ref= "Jdkserializationredisserializer"/>
</bean>
With the above configuration, you can operate the Redis database, the following is a code for a simple test connection
ApplicationContext applicationcontext=new classpathxmlapplicationcontext ("Applicationcontext.xml");
Redistemplate redistemplate= (redistemplate) Applicationcontext.getbean ("Redistemplate");
Redistemplate.opsforvalue (). Set ("Role1", "Zhangsan");
Sessioncallback sessioncallback=new Sessioncallback () {//Sessioncallback mainly because set and get operations when they may not access a Redis connection, and call them through the Sessioncallback interface. You can use only one Redis connection
@Override
Public Object Execute (redisoperations redisoperations) throws DataAccessException {
Redisoperations.opsforvalue (). Set ("Role2", "Lisi");
Return Redisoperations.opsforvalue (). Get ("Role2");
}
};
String Aa=redistemplate.execute (sessioncallback). toString ();
SYSTEM.OUT.PRINTLN (AA);
Redis Configuration and beginner operations in spring