【问题标题】:Error creating bean with name 'consumerController': Injection of resource dependencies failed创建名为“consumerController”的 bean 时出错:注入资源依赖项失败
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 嘿,我试过你的解决方案。我也更新了问题中的xml。仍然面临同样的错误:创建名称为“consumerController”的bean时出错:通过字段“memcachedClient”表达的不满足依赖项:没有为依赖项[net.spy.memcached.MemcachedClient]找到类型[net.spy.memcached.MemcachedClient]的合格bean : 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者
猜你喜欢
  • 2016-05-15
  • 1970-01-01
  • 2015-12-06
  • 2019-06-10
  • 2014-06-09
  • 2012-04-25
  • 2018-06-22
  • 2014-11-24
  • 2015-06-12
相关资源
最近更新 更多