创建名为"consumerController"的 Bean 时出错:注入资源依赖关系失败



我正在尝试使用Spring MVC将Rest api与memcached连接起来,以设置和获取我从API获得的memcached上的数据。 当前收到此错误:

SEVERE:上下文初始化失败 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为"消费者控制器"的 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: No 找到 [net.spy.memcached.MemcachedClient] 类型的限定 bean dependency [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 {
}

消费者初始值设定项:

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>

请告诉我我做错了什么以及如何解决此错误

在 Bean 创建中,您将类作为控制器类。所以你正在创建一个控制器的bean,而不是MemchachedClient。 将其更改为net.spy.memchached.MemchachedClient。

最新更新