我也尝试将第三方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)}
...
答案 0 :(得分:2)
您应该在类似的应用程序中添加@Import
annotation。
例如:
@Import(ApplicationConfiguration.class)
public class Application {
}
注意:参见@ M.Prokhorov评论说,ZMQ.Socket
被try-with-resource语句
答案 1 :(得分:0)
你创建了一个名为mqSocket的bean,所以我想你必须使用@Qualifier
注释;尝试以这种方式更改您的代码:
@RestController
public class MyRestController {
@Autowired
@Qualifier("mqSocket")
private ZMQ.Socket mqSocket;
我希望它有用
安吉洛