找不到 Redis HashOperations 依赖项?
Redis HashOperations dependency not found?
我正在学习 Spring 关于如何为 Redis 使用散列映射的教程。 http://docs.spring.io/spring-data/redis/docs/1.8.4.RELEASE/reference/html/#redis.hashmappers.root
这是我的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);
}
}
当我将此 class 注册为 bean 并在此处启动服务器时,我的错误消息是:
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.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)
我该如何解决这个问题?我完全按照 Spring 教程所说
我已经解决了这个问题。 HashOperations 是作为 RedisTemplate 一部分的众多操作视图之一。要使用其中一个视图,您必须将视图声明为依赖项,然后注入 RedisTemplate。
我是这样做的:
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);
}
}
我正在学习 Spring 关于如何为 Redis 使用散列映射的教程。 http://docs.spring.io/spring-data/redis/docs/1.8.4.RELEASE/reference/html/#redis.hashmappers.root
这是我的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);
}
}
当我将此 class 注册为 bean 并在此处启动服务器时,我的错误消息是:
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.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)
我该如何解决这个问题?我完全按照 Spring 教程所说
我已经解决了这个问题。 HashOperations 是作为 RedisTemplate 一部分的众多操作视图之一。要使用其中一个视图,您必须将视图声明为依赖项,然后注入 RedisTemplate。
我是这样做的:
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);
}
}