gpt4 book ai didi

spring-boot - LettuceConnectionFactory对redis和springboot是否有版本限制?

转载 作者:IT王子 更新时间:2023-10-29 06:04:56 26 4
gpt4 key购买 nike

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

开发环境:

Springboot:2.1.0.release

redis:3.2.8

jdk8.

Java 代码

@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)

最佳答案

NullPointerException 发生是因为 LettuceConnectionFactory 尚未初始化。它应该由 Spring Framework 调用 afterPropertiesSet() 初始化,这是标准的 bean 生命周期方法之一。由于 RedisConfig.lettuceConnectionFactoryTest() 上缺少 @Bean 注释,您的 LettuceConnectionFactory 不是 bean,因此该方法未被调用。

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

关于spring-boot - LettuceConnectionFactory对redis和springboot是否有版本限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53128219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com