我尝试将第三方 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/