我试图使用spring将Rest与memcached连接起来,以设置和获取memcached上的数据,这是我从API中获得的。当前正在获取此错误:
严重:上下文初始化失败,org.springframework.beans.factory.UnsatisfiedDependencyException:错误创建名为“使用者控制器”的bean :通过字段‘memcachedClient’表示的不满意的依赖关系:没有为dependency net.spy.memcached.MemcachedClient:期望值至少找到一个net.spy.memcached.MemcachedClient类型的限定bean,该bean可以作为此依赖项的自动选择。依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)};嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:,没有为dependency找到net.spy.memcached.MemcachedClient类型的限定bean :预期至少有一个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 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>请建议我在这方面做错了什么,以及如何解决这个错误。
发布于 2018-10-11 16:14:20
在bean创建中,您将类作为控制器类。因此,您正在创建控制器的bean,而不是MemchachedClient。将其更改为net.spy.memchached.MemchachedClient。
https://stackoverflow.com/questions/52754169
复制相似问题