当前位置: 首页 > 知识库问答 >
问题:

LettuceConnectionFactory是否对redis和SpringBoot有版本限制?

袁晋鹏
2023-03-14

该项目需要一个自定义的RedisConnectionFactory,并发现了一个问题:当使用LettuceConnectionFactory时,运行时总是报告java.lang.NullPointerException,而JedisConnectionFactory可以通过测试。我想LettuceConnectionFactory是否对redis和Springboot有版本限制?

开发环境:

SpringBoot:2.1.0.发行版

Redis:3.2.8

@Component
@Configuration
public class RedisConfig {

    public LettuceConnectionFactory lettuceConnectionFactoryTest(){
        return new LettuceConnectionFactory(new RedisStandaloneConfiguration("127.0.0.1", 6379));
    }

    public JedisConnectionFactory jedisConnectionFactoryTest(){
        return new JedisConnectionFactory(new RedisStandaloneConfiguration("127.0.0.1", 6379));
    }

}
@Autowired
private RedisConfig redisConfig;

@Autowired
private StringRedisTemplate redisTemplate;

@Test
public void test(){

    redisTemplate.setConnectionFactory(redisConfig.lettuceConnectionFactoryTest());
    ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
    valueOperations.set("test", "test123");
    System.out.println(valueOperations.get("test"));

}
java.lang.NullPointerException
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1085)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1065)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedConnection(LettuceConnectionFactory.java:865)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getConnection(LettuceConnectionFactory.java:340)
at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:132)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:95)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:82)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:211)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:236)
at com.test.infrastructure.InfrastructureApplicationTests.test(InfrastructureApplicationTests.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

共有1个答案

朱兴学
2023-03-14

出现NullPointerException是因为LettuceConnectionFactory尚未初始化。它应该由Spring框架调用afterPropertiesSet()初始化,这是标准bean生命周期方法之一。由于redisconfig.lettuceConnectionFactoryTest()上缺少@bean注释,因此没有调用该方法,因为LettuceConnectionFactory不是bean。

redisconfig.lettuceConnectionFactoryTest()上添加@bean应该可以解决您的问题。它还允许您直接注入LettuceConnectionFactory(到测试中的@autowired字段中),而不是注入redisconfig然后调用LettuceConnectionFactory()

 类似资料:
  • 我试图用1.5.15.release版本在Spring Boot项目中配置LettuceConnectionFactory。

  • 我正在编写一个在输入面板中包含多个JTextFields和2个JTextAreas的程序。我有一个提交按钮在底部。我设置了它,所以当用户在每个字段中键入某些内容(包括JTextAreas)并按下Enter键时,它会更新一个文本文件,当他们按下submit按钮时,它会更新该文件,然后在本地目录中输出该文件的新版本。 如果用户在任一字段中按Enter键,它将验证他们的输入,但是,我希望在他们按subm

  • 问题内容: 具体说说(服务器端)V8,并假设我不担心准确性,因为我可以检测和补偿它,那么我可以使用setTimeout 逐字 间隔设置几千个相对简单的超时,而无需面对任何其他限制除了RAM以外?如果我要使用一个在任何给定时间可能有数千个计划的超时的系统,我应该注意什么吗? 作为记录,我已经阅读了John’s Resig关于Javascript计时器如何工作的出色文章,因此无需指出那里已经介绍的内容

  • 问题内容: 对于跨进程的数据通信,我打算使用Redis列表。生产者推送到列表,而一组使用者使用BRPOP消费列表内容。 为了限制列表的大小无限增长,我想将列表大小限制为固定值(例如1万个项目)。我很惊讶地没有找到像BLPUSH或BRPUSH这样的等效命令。这是Redis员工故意遗漏的吗? 因此,我假设我必须在推送之前使用Watch / multi创建一个Txn来检查列表大小。这是正确的方法还是可用

  • 问题内容: 这个问题出现在Spring类中,该类具有相当长的类名称。类名长度的语言是否有限制? 问题答案: 在Java语言规范指出标识符的长度不受限制。 但是实际上,文件系统将限制生成的文件名的长度。

  • 问题内容: 我想知道是否有一种方法可以通过我的应用程序进行一次往返操作来在Redis中执行此操作: 对于给定的键,其可能的值是范围内的任何整数。基本上,它具有上下边界。 当发出or 命令时(例如), 仅 当结果值没有超出范围 时才 执行。 我需要这个操作是原子的,并且我想知道是否有一种方法可以避免为此编写Lua脚本。 谢谢。 问题答案: 这个答案可能不是您所期望的。但是我不得不说,Lua脚本是非常