我工作在OS X上的Java 7,春季3.2,jedis 2.1.0,弹簧数据redis的1.1.1,试图让最赤裸的Redis设置了默认的配置redis的工作.意思是我没有在redis.conf文件中添加任何内容.当我做
redis-server
Run Code Online (Sandbox Code Playgroud)
它说它已经启动并准备接受端口6379上的连接.
起初,我试图与该RedisTemplate和JedisConnectionFactory注解的bean,但春天抱怨它无法创建或找到这些秘密,所以我就是这么做的.也许这表明了一个更基本的问题.所以我用下面稍长的版本来做,但这至少看起来是创建Redis和Jedis组件.
这是我的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class RedisTest {
@Configuration
static class ContextConfiguration {
}
RedisTemplate<String, String> template;
private JedisConnectionFactory getJedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName("localhost");
factory.setPort(6379);
factory.setUsePool(true);
return factory;
}
private RedisTemplate<String, String> getRedisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(getJedisConnectionFactory());
return redisTemplate;
}
@Test
public void testRedis() {
System.out.println("testing redis ");
template = getRedisTemplate();
template.opsForValue().set("Key", "Value");
String value = template.opsForValue().get("Key");
System.out.println("got value : " + value);
}
}
Run Code Online (Sandbox Code Playgroud)
并且错误堆栈跟踪的顶部是
java.lang.NullPointerException
java.lang.NullPointerException
at org.springframework.data.redis.core.AbstractOperations.rawValue(AbstractOperations.java:110)
at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:166)
at com.mycompany.storage.RedisTest.testRedis(RedisTest.java:46)
Run Code Online (Sandbox Code Playgroud)
问题是redisTemplate和jedisConnectionFactory都需要调用afterPropertiesSet().通常这是由Spring配置调用的,但由于这对我不起作用,因此必须明确调用它.
还有,这些行
factory.setHostName("localhost")
factory.setPort(6379)
factory.setUsePool(true)
Run Code Online (Sandbox Code Playgroud)
是不必要的,因为它们是默认值.
| 归档时间: |
|
| 查看次数: |
1860 次 |
| 最近记录: |