Search code examples
javaspringredisspring-data-redis

Redis HashOperations dependency not found?


I'm following a Spring tutorial on how to use hash mapping for Redis. http://docs.spring.io/spring-data/redis/docs/1.8.4.RELEASE/reference/html/#redis.hashmappers.root

Here is my class:

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;

import com.commercehub.jackson.datatype.mongo.MongoModule;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import model.Event;

public class HashMapping {

@Autowired
private HashOperations<String, byte[], byte[]> hashOperations;

private ObjectMapper objectMapper = new ObjectMapper();

    public void writeHash(String key, Event event) throws JsonProcessingException {
    objectMapper.registerModule(new MongoModule());
    String json = objectMapper.writeValueAsString(event);

    Map<byte[], byte[]> mappedHash = new HashMap<byte[], byte[]>();
    mappedHash.put(key.getBytes(), json.getBytes());

    hashOperations.putAll(key, mappedHash);
    }
}

When I register this class as bean and start the server here is my error message:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hashMapping': Unsatisfied dependency expressed through field 'hashOperations': No qualifying bean of type [org.springframework.data.redis.core.HashOperations] found for dependency [org.springframework.data.redis.core.HashOperations<java.lang.String, byte[], byte[]>]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.redis.core.HashOperations] found for dependency [org.springframework.data.redis.core.HashOperations<java.lang.String, byte[], byte[]>]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

How do I fix this? I'm following exactly like the Spring tutorial says


Solution

  • I have solved the issue. HashOperations is one of the many operational views that is part of the RedisTemplate. To use one of the views, you must declare the view as a dependency and then inject the RedisTemplate.

    Here is how I did it:

    import java.util.Map;
    
    import javax.annotation.Resource;
    
    import org.springframework.data.redis.core.HashOperations;
    import org.springframework.data.redis.hash.HashMapper;
    import org.springframework.data.redis.hash.ObjectHashMapper;
    
    import foo.Person;
    
    public class HashMapping {
    
    @Resource(name = "redisTemplate")
    private HashOperations<String, byte[], byte[]> hashOperations;
    
    private HashMapper<Object, byte[], byte[]> mapper = new ObjectHashMapper();
    
    public void writeHash(String key, Person person) {
    
        Map<byte[], byte[]> mappedHash = mapper.toHash(person);
        hashOperations.putAll(key, mappedHash);
    }
    
    public Person loadHash(String key) {
    
        Map<byte[], byte[]> loadedHash = hashOperations.entries(key);
        return (Person) mapper.fromHash(loadedHash);
    }
    }