gpt4 book ai didi

java - redisTemplate 上的 Spring Redis 空指针异常

转载 作者:IT王子 更新时间:2023-10-29 06:05:08 25 4
gpt4 key购买 nike

您好,我正在使用 java-redis 与 jedis 集成,我在尝试设置项目时遇到了这个问题。我是 Spring 的新手,所以初始设置可能存在一些问题。这是我正在尝试使用的代码:

RedisConfig.java

@Configuration
public class RedisConfig {

@Bean
public RedisConnectionFactory jedisConnectionFactory() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(1000);

JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
jedisConnectionFactory.setHostName(Properties.getInstance().getPropertyValueReader().getAWSRedisEndPoint());
jedisConnectionFactory.setPort(6379);
return jedisConnectionFactory;
}

@Bean
public <K, V> RedisTemplate<String, V> getRedisTemplate() {
JdkSerializationRedisSerializer jackson2JsonRedisSerializer = new JdkSerializationRedisSerializer();
RedisTemplate<String, V> redisTemplate = new RedisTemplate<String, V>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
return redisTemplate;
}

}

CacheRepository.java

public interface CacheRepository<T, V> {
V get(Object key);

void add(Object redisKey, Object key, T value);

void add(T key, V value);

void expire(Object key);
}

CacheRepositoryImpl.java

@Component
public class CacheRepositoryImpl<T, V> implements CacheRepository<T, V> {

@Autowired
protected RedisTemplate<String, V> redisTemplate;

@Override
public V get(Object key) {
}

@Override
public void expire(Object key) {
}

@Override
public void add(Object redisKey, Object key, T value) {
}

@Override
public void add(T key, V value) {
}
}

VendorInformationCacheRepository.java

public class VendorInformationCacheRepository extends CacheRepositoryImpl<String, HashMap<String, List<Vehicles>>> {

private static final String KEY = "Vendors";

@Override
public void add(String key, HashMap<String, List<Vehicles>> value){
System.out.println(redisTemplate); // this prints null
redisTemplate.opsForHash().put(KEY, key, value);
}

BaseController.java

VendorInformationCacheRepository vicr = new VendorInformationCacheRepository();
vicr.add("testKey", myNewList);

VendorInformationCacheRepository.java redisTemplate.opsForHash().put(KEY, key, value); 给出空指针异常。这是错误的堆栈跟踪:

java.lang.NullPointerException

at com.moveinsync.cache.CacheRepositoryImpl.add(CacheRepositoryImpl.java:35)
at com.moveinsync.controllers.BaseController.redisTest(BaseController.java:97)
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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

任何帮助将不胜感激。提前致谢。干杯

最佳答案

找出我做错了什么。在我的基本 Controller 内部,我将对象初始化为:

VendorInformationCacheRepository vicr = new VendorInformationCacheRepository();

这将导致丢失在 bean 内部完成的所有 Autowiring 。我所要做的就是调用类上的方法,因为该类已经 Autowiring 。

关于java - redisTemplate 上的 Spring Redis 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45689354/

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