Java NoSuchBeanDefinitionException:没有内部类类型的限定bean
我尝试将第三方bean添加到我的应用程序:Java NoSuchBeanDefinitionException:没有内部类类型的限定bean,java,spring,spring-mvc,spring-boot,autowired,Java,Spring,Spring Mvc,Spring Boot,Autowired,我尝试将第三方bean添加到我的应用程序: @Configuration @ComponentScan(...) public class ApplicationConfiguration { @Bean(name = "mqSocket") public ZMQ.Socket startServer() { try (ZMQ.Context ctx = ZMQ.context(1); ZMQ.Socket publisher = ct
@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)}
...
您创建了一个名为mqSocket的bean,因此我猜您必须使用
@Qualifier
注释;尝试以以下方式更改代码:
@RestController
public class MyRestController {
@Autowired
@Qualifier("mqSocket")
private ZMQ.Socket mqSocket;
我希望它有用
Angelo您应该在应用程序类中添加
例如:
注意:cf.@M.Prokhorov comments,
ZMQ.Socket
由try with resource语句关闭,这不应该是问题。支持内部类。是否调用了startServer
方法?是否正在加载配置?尝试查找一个虚拟bean(简单字符串),并查看它是否从相同的配置加载。在startServer()
返回之前,您的ZMQ.Socket
已关闭。这是你期望的行为吗?@Nicolas,事实上not@Nicolas谢谢,它起作用了。请加上答案,我会接受的,这对我没有帮助
@Import(ApplicationConfiguration.class)
public class Application {
}