【发布时间】:2019-03-16 04:31:10
【问题描述】:
我正在尝试使用 spring MVC 将 Rest api 与 memcached 连接,以设置和获取我从 API 获得的 memcached 上的数据。 目前收到此错误:
严重:上下文初始化失败 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“consumerController”的 bean 时出错:不满意 通过字段“memcachedClient”表示的依赖项:无限定 为依赖找到了类型为 [net.spy.memcached.MemcachedClient] 的 bean [net.spy.memcached.MemcachedClient]:预计至少有 1 个 bean 有资格成为此依赖项的自动装配候选者。依赖 注释: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 找到类型为 [net.spy.memcached.MemcachedClient] 的合格 bean 依赖项 [net.spy.memcached.MemcachedClient]:预计至少 1 有资格作为此依赖项的自动装配候选者的 bean。 依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
消费者控制器:
import net.spy.memcached.MemcachedClient;
@RestController
public class consumerController {
@Autowired
private MemcachedClient memcachedClient;
@RequestMapping(value="/queues/{queueName}/thread-counts/{threadCount}", method = RequestMethod.GET)
public void setThreadCount(@PathVariable String queueName, @PathVariable int threadCount) {
System.out.println("Queue name is "+queueName);
System.out.println("Thread count is "+threadCount);
//memcachedClient.set(queueName, 0, threadCount);
memcachedClient.add(queueName, 0, threadCount); //Adding value to memcached
}
@RequestMapping(value="/queues/{queueName}/thread-counts", method = RequestMethod.GET)
public void getThreadCount(@PathVariable String queueName) {
System.out.println("Queue value is"+queueName);
int threadCount = (Integer)memcachedClient.get(queueName);//Getting value from memcached
System.out.println("Thread count for " + queueName + " is " + threadCount);
}
}
消费者配置:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "msg91.consumers.consumer")
public class consumerConfiguration {
}
ConsumerInitializer:
public class consumerInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { consumerConfiguration.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Spring-dispatcher-servlet.xml:
<bean id="memcachedClient"
class="net.spy.memchached.MemchachedClient">
<property name="servers" value="127.0.0.1:11211"/>
<property name="protocol" value="BINARY"/>
Pom.xml:
<dependency>
<groupId>com.google.code.simple-spring-memcached</groupId>
<artifactId>spymemcached-provider</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.google.code.simple-spring-memcached</groupId>
<artifactId>spring-cache</artifactId>
<version>3.2.0</version>
</dependency>
请建议我在这方面做错了什么以及如何解决此错误
【问题讨论】:
-
有人请帮忙,我无法弄清楚问题
标签: java spring rest spring-mvc servlets