繁体   English   中英

NoSuchBeanDefinitionException:没有内部类的合格 bean

[英]NoSuchBeanDefinitionException: No qualifying bean of type for inner class

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

我尝试像这样自动装配:

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

例如。:

@Import(ApplicationConfiguration.class)
public class Application {

} 

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

您创建了一个名为 mqSocket 的 bean,所以我猜您必须使用@Qualifier注释; 尝试以这种方式更改您的代码:

@RestController
public class MyRestController {    

    @Autowired
    @Qualifier("mqSocket")
    private ZMQ.Socket mqSocket;

我希望它有用

安杰洛

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM