当前位置: 首页 > 知识库问答 >
问题:

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

慕震博
2023-03-14

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

严重:上下文初始化失败org . spring framework . beans . factory . unsatisfieddependencyexception:创建名为“consumerController”的bean时出错:通过字段“memcachedClient”表示的未满足的依赖关系:未找到类型为[net . spy . memcached . memcached client]的合格bean用于依赖关系[net . spy . memcached . memcached client]:应至少有一个bean可作为此依赖关系的autowire候选项。依赖项批注:{ @ org . spring framework . beans . factory . annotation . auto wired(required = true)};嵌套异常为org . spring framework . beans . factory . nosuchbeandefimentionexception:未找到符合依赖关系[net . spy . memcached . memcached client]的[net . spy . memcached client]类型的bean:应至少有一个bean符合此依赖关系的autowire候选项。依赖项批注:{ @ org . spring framework . beans . factory . annotation . auto wired(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>

请告诉我我在这方面做错了什么,以及如何解决这个错误

共有1个答案

谷翰飞
2023-03-14

在bean创建中,您将该类作为您的控制器类。因此,您正在创建控制器的bean,而不是MemchachedClient。改为net . spy . memchached . memchached client。

 类似资料: