创建名称为'consumerController'的bean时出错:注入资源依赖项失败

时间:2018-10-11 07:03:47

标签: java spring rest spring-mvc servlets

我正在尝试使用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)}

ConsumerController:

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);
    }
}

ConsumerConfiguration:

@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>

请向我建议我在这方面做错了什么以及如何解决此错误

1 个答案:

答案 0 :(得分:0)

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

相关问题