Spring简单整合redis

项目环境:maven+sping4.2.2+redis3.2.8,

首先安装redis,确保redis正常运行,redis-cli能正常运行,运行后输入ping能够收到pong,向项目中pom.xml引入相关架包

		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.7.8.RELEASE</version>
		</dependency>

架包导入后开始配置redis,首先是redis的配置文件redis.properties(下面只是一些简单的配置,还有更加复杂的配置,这个需要百度查找)

       # Redis settings  
redis.host=	
redis.port=6379
redis.password=
redis.maxIdle=300
redis.maxActive=600 
redis.maxWait=1000 
redis.testOnBorrow=true  
redis.usePool=true

       然后在spring配置文件applicationContext.xml中配置redis

	<context:property-placeholder location="classpath:redis.properties" />
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.maxActive}" />
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxWaitMillis" value="${redis.maxWait}" />
		<!-- 设置为true表示要进行获取链接之前的验证  而且需要redis.conf配置里面的bind或者requirepass -->
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>

	<bean id="connectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="${redis.host}" />
		<property name="port" value="${redis.port}" />
		<property name="password" value="${redis.password}" />
		<property name="poolConfig" ref="poolConfig" />
	</bean>
	<bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
  	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
     	   <property name="connectionFactory" ref="connectionFactory" />
    	</bean>

在jar包配置的时候,会出现一下异常

Caused by: java.lang.NoSuchMethodError: org.springframework.core.serializer.support.DeserializingConverter.<init>(Ljava/lang/ClassLoader;)V
	at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.<init>(JdkSerializationRedisSerializer.java:54)
	at org.springframework.data.redis.core.RedisTemplate.afterPropertiesSet(RedisTemplate.java:119)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
把spring4.2.0改成4.2.2(或者更高版本都行),一开始我是用的spring4.2.0版本,出现这个异常后将spring版本换成了4.2.2

弄好上面以后,尝试运行一下,如果没有报错,说明ja和配置相关的东西初步确定没有错,

接下来就是写java  dao类,我首先定义了一个RedisDao接口,代码如下

import java.io.Serializable;

public interface RedisDao<K extends Serializable ,V  extends Serializable> {
	int add(K key,V value);
	V get(K key);
	
}
因为redis读写要求Serializable接口(读和写基本都要求实现Serializable接口的),

然后定义AbstractRedisDao 作为一个抽象类实现redisDaoj接口

package com.website.dao.redis.impl;

import java.io.Serializable;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import com.website.dao.redis.RedisDao;

@Repository
public abstract class AbstractRedisDao<K extends Serializable ,V  extends Serializable> implements RedisDao<K, V> {

	@Resource
	RedisTemplate<K, V> redisTemplate;

	public int add(K key, V value) {
		ValueOperations<Serializable, Serializable> opsForValue =(ValueOperations<Serializable, Serializable>) redisTemplate.opsForValue();
		boolean result = opsForValue.setIfAbsent(key, value);
		return  result==true?1:0;
	}
	public V get(K key) {
		return (V)redisTemplate.opsForValue().get(key);
	}
}
然后使用UserRedisDaoImpl继承AbstractRedisDao 实现UserRedisDao接口,然后开始测试类,使用spring测试

测试代码如下:

package com.website.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.website.dao.redis.UserRedisDao;
import com.website.entity.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class RedisTest {
	@Autowired
	private UserRedisDao<String, User>  userRedisDao;
	@Test
	public void redis(){
 		User user = new User("name", "account", "passwd");
 		int result = userRedisDao.add(user.getName(), user);
 		System.out.println(result);
 		User user2 = (User) userRedisDao.get("name");
		System.out.println(user2.getAccount());
 	}
}

上面的测试类完全能够存和取出,这里只是简单的方法,其他的方法可以从api获取,

目前为止就简单实现了spring简单整合redis
在整合过程中遇到错误:
1.大概意思就是不能连接服务器,(redis配置文件好像有空格多余,)
2.redis拒绝连接,redis.conf中注释掉 bind  或者将当前IP绑定在上面
3.当redis.testOnBorrow=true  设置时,redis.conf必须绑定ip或者设定密码登录,因为我上面没有绑定ip所以设置秘密登录,在配置文件找requirepass .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值