您好我正在使用java-redis与jedis集成,我在尝试设置项目时想出了这个问题。我是新手,因此初始设置可能存在一些问题。这是我正在尝试使用的代码:
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)
任何帮助都将受到高度赞赏。提前致谢。干杯
答案 0 :(得分:1)
弄清楚我做错了什么。在我的基本控制器内部,我将对象初始化为:
VendorInformationCacheRepository vicr = new VendorInformationCacheRepository();
这将导致丢失在bean内部完成的所有自动装配。我所要做的就是在类已经自动装配时调用类上的方法。
答案 1 :(得分:0)
基于此行
System.out.println(redisTemplate); // this prints null
您在连接Redis模板时遇到问题。我不认为你的泛型bean声明可以胜任这项工作。相反,将bean定义为RedisTemplate<String,Object>
:
@Bean
public RedisTemplate<String, Object> redisTemplate() {
...
}
从简单的集成测试开始,该测试将确认您可以将正确的RestTemplate
实例连接到您的存储库。
答案 2 :(得分:0)
我遇到了同样的问题,我的redisTemplate为null,最后以下代码对我有用,我在 redisTemplate 方法中添加了 @Autowire 并注入 JedisConnectionFactory 作为参数。
@Bean(name = "factory")
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
...
return jedisConFactory;
}
@Bean(name = "common")
@Autowired
public RedisTemplate<String, Object> redisTemplate(@Qualifier("factory") JedisConnectionFactory jedisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
....
return redisTemplate;
}
然后可以按以下方式使用它,
@Autowired
@Qualifier("common")
RedisTemplate<String, Object> redisTemplate;
或
private final RedisTemplate<String, Object> redisTemplate;
@Autowired
public TestRedisController(@Qualifier("common") RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}