2

Hi I am working with java-redis integration with jedis and I came up with this problem when trying to setup project. I am new to spring so there might be some issues with initial setup. This is the code that I am trying to work with:

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

In VendorInformationCacheRepository.java redisTemplate.opsForHash().put(KEY, key, value); gives null pointer exception. This is the stack trace of the error:

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)

Any help would be highly appreciated. Thanks in advance. Cheers

6
  • where is code with 'BaseController.redisTest' ? Commented Aug 15, 2017 at 8:40
  • @JacekCz Made an edit to the post Commented Aug 15, 2017 at 8:43
  • Mark VendorInformationCacheRepository as @Repository and place to be available for package scan Commented Aug 15, 2017 at 8:44
  • @StanislavL that did not help :( Commented Aug 15, 2017 at 8:46
  • where is the code at CacheRepositoryImpl line 35? actual exception line? Commented Aug 15, 2017 at 9:03

3 Answers 3

6

Figured out what I was doing wrong. Inside of my base controller I was initializing the object as :

VendorInformationCacheRepository vicr = new VendorInformationCacheRepository();

This would lead to losing all the autowiring done inside of the bean.All I had to do was call the method on the class as the class was already autowired.

Sign up to request clarification or add additional context in comments.

Comments

0

Based on this line

System.out.println(redisTemplate); // this prints null

you have issues with wiring the Redis template. I don't think that your bean declaration with generics does the job. Instead, define the bean as RedisTemplate<String,Object>:

  @Bean
  public RedisTemplate<String, Object> redisTemplate() {
    ...
  } 

Start with simple integration test that will confirm that you could wire the proper RestTemplate instance into your repository.

2 Comments

Did you create the test to reproduce if the problem with the bean injection configuration?
No, I am actually new to spring so I don't know how to write that test.
0

I encountered the same problem , my redisTemplate is null, finally the following codes work for me, I added @Autowire to the redisTemplate method and inject JedisConnectionFactory as a parameter.

  @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;
    }

Then can use it like below,

 @Autowired
 @Qualifier("common")
 RedisTemplate<String, Object> redisTemplate;

or

 private final RedisTemplate<String, Object> redisTemplate;

  @Autowired
  public TestRedisController(@Qualifier("common") RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.