Recently, I am using websocket to develop a chat system, and I plan to use redis to store chat records.
First import the spring-integrated redis package spring-data-redis-1.6.2.RELEASE.jar, the redis java driver package jedis-2.9.0.jar
spring configuration file
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="personal.mario" /> <context:property-placeholder location="classpath:redis.properties" /> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="maxTotal" value="${redis.pool.maxActive}" /> <property name="MaxWaitMillis" value="${redis.pool.maxWait}" /> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> </bean> <bean id="jdsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}" /> <property name="port" value="${redis.port}" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jdsConnectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> </bean> </beans>redis configuration parameters
#Maximum number of objects to allocate redis.pool.maxActive=1024 #Maximum number of objects that can keep the idle state redis.pool.maxIdle=200 #When there is no object returned in the pool, the maximum waiting time redis.pool.maxWait=1000 #When calling the borrow Object method, whether to check the validity redis.pool.testOnBorrow=true #IP redis.host=localhost #Port redis.port=6379The dao layer is injected into redisTemplate
package personal.mario.dao.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.ListOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.stereotype.Repository; import personal.mario.bean.Message; import personal.mario.dao.MessageDao; @Repository("messageDao") public class MessageDaoImpl implements MessageDao { @Autowired private RedisTemplate<String, Message> redisTemplate; public RedisTemplate getRedisTemplate() { return redisTemplate; } public void setRedisTemplate(RedisTemplate<String, Message> redisTemplate) { this.redisTemplate = redisTemplate; } @Override public void save(Message message) { redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Message>(Message.class)); redisTemplate.afterPropertiesSet(); ListOperations<String, Message> ops = redisTemplate.opsForList(); ops.leftPush("chatRecord", message); } @Override public List<Message> getList(String key) { ListOperations<String, Message> ops = redisTemplate.opsForList(); return ops.range("chatRecord", 0, ops.size("chatReocrd")); } }
It can be seen that the serialization of the key value stored in redis is configured in the configuration file, because the key is generally a string, so it can be fixed. But the value may be a string or an object, so it is configured in the code, which is more flexible.
Dependency injection using annotations in websocket failed, and finally ContextLoader.getCurrentWebApplicationContext().getBean("messageDao") was used to inject successfully.