最近业余在使用websocket开发一个聊天系统,打算使用redis存储聊天记录。
首先导入spring集成redis的包spring-data-redis-1.6.2.RELEASE.jar, redis java驱动包jedis-2.9.0.jar
spring配置文件
<?xml version="1.0" encoding="UTF-8"?>redis配置参数
<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>
#最大分配的对象数dao层注入redisTemplate
redis.pool.maxActive=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#IP
redis.host=localhost
#Port
redis.port=6379
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"));
}
}
可以看到对于redis存储key值的序列化是在配置文件中配置的,因为key一般都是string,所以可以固定。但是value可能是string,也可能是object,所以在代码中进行了配置,这样比较灵活。
在websocket中使用注解进行依赖注入失败,最后采用ContextLoader.getCurrentWebApplicationContext().getBean("messageDao")注入成功.