gpt4 book ai didi

java - NoSuchBeanDefinitionException : No qualifying bean of type for inner class

转载 作者:行者123 更新时间:2023-11-30 02:35:33 24 4
gpt4 key购买 nike

我尝试将第三方 bean 添加到我的应用程序中:

@Configuration
@ComponentScan(...)
public class ApplicationConfiguration {

@Bean(name = "mqSocket")
public ZMQ.Socket startServer() {
try (ZMQ.Context ctx = ZMQ.context(1);
ZMQ.Socket publisher = ctx.socket(ZMQ.PUB)) {
publisher.bind("tcp://*:5556");
return publisher;
}
}
}

我尝试像这样 Autowiring :

@RestController
public class MyRestController {

@Autowired
private ZMQ.Socket mqSocket;

但它打印以下内容:

java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myRestController': Unsatisfied dependency expressed through field 'mqSocket'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.zeromq.ZMQ$Socket' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.zeromq.ZMQ$Socket' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
...

最佳答案

您应该添加 @Import annotation在您的应用程序类中。

例如:

@Import(ApplicationConfiguration.class)
public class Application {

}

注:参见@M.Prokhorov 评论,ZMQ.Socket 由 try-with-resource 语句关闭

关于java - NoSuchBeanDefinitionException : No qualifying bean of type for inner class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43205347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com