1. 程式人生 > >spring整合redis,序列化物件,以及websocket依賴注入

spring整合redis,序列化物件,以及websocket依賴注入

      最近業餘在使用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"?>
<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配置引數
#最大分配的物件數
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
dao層注入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"));
	}

}

可以看到對於redis儲存key值的序列化是在配置檔案中配置的,因為key一般都是string,所以可以固定。但是value可能是string,也可能是object,所以在程式碼中進行了配置,這樣比較靈活。

在websocket中使用註解進行依賴注入失敗,最後採用ContextLoader.getCurrentWebApplicationContext().getBean("messageDao")注入成功.