Spring-集成网关无法自动装配,找不到合格的 bean

Spring-integration gateway cannot be autowired, no qualifying bean found

我正在尝试将 spring 批处理的输出发送到 RabbitMQ。为了避免对 Rabbitmq 的硬依赖,我按照 中的建议使用了 spring-integration。在我决定通过 Spring.

自动装配或实例化 bean 之前,我的一切工作正常,消息被推送到 RabbitMQ
<context:component-scan base-package="com.test.*"/> 
<channel id="input" />

<rabbit:connection-factory id="connectionFactory" />

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
    exchange="testExchange" routing-key="foo.bar" />
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="testQueue" id="timeseriesQueue" />
<rabbit:topic-exchange name="testExchange"
    id="testExchange">
    <rabbit:bindings>
        <rabbit:binding queue="testQueue" pattern="foo.*" />
    </rabbit:bindings>
</rabbit:topic-exchange>

<!-- Send message to rabbitmq -->
<gateway id="testGateway"
    service-interface="com.test.TestService"
    default-request-channel="input" />


<amqp:outbound-channel-adapter channel="input"
    amqp-template="amqpTemplate" exchange-name="testExchange"
    routing-key="foo.bar" />

testService 只是一个带有方法 sendMessage 的接口。

spring 批处理作业有一个 itemWriter,它使用网关 bean 写入 RabbitMQ

在 itemWriter 的 write 方法中,当我使用 ApplicationContext 实例化 bean 时,如下所示,它运行良好

AbstractApplicationContext ctx =
                new ClassPathXmlApplicationContext("META-INF/spring/integration/rabbit.xml");

     TestService service = ctx.getBean("testGateway", TestService.class);
    service.sendMessage(items);
    ctx.close();

但是,当我尝试自动装配它时,如下所示:

@Autowired
TestService service;

我得到以下异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.test.TestService com.test.TransportItemWriter.testGateway; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.TestService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.TestService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 24 more

我试着环顾四周,但网关的大多数示例(在 spring-集成示例中)都是通过 ApplicationContext 调用它的。我找到了 http://java.dzone.com/articles/spring-integration-gateways-1,并添加了我认为缺少的任何注释,但无法使其正常工作。

非常感谢任何指点。

Autowiring 在功能上等同于从上下文中获取 bean(除非有多个实例,在这种情况下,您需要正确的变量名,或者 @Qualifier),但这会给您一个不同的错误。网关是否与 ItemWriter 在同一上下文中?

`META-INF/spring/integration/rabbit.xml`

暗示它在它自己的上下文中。您 <import/> 了解您的工作环境吗?

我通常通过为 org.springframework 打开调试日志记录来调试此类问题;它为 bean 连接发出大量日志。

另一个好的调试技巧是@Autowire ApplicationContext 并打印出ctx.getBeanFactory().toString().

我最近也在处理类似的问题。解决方案是在配置中添加新注释:

@IntegrationComponentScan("path.to.your.gateway")

It's a old question I know, but I had a Similar Issue:

I would use a anotated class @MessagingGateway interface called MySFTPUploadGateway.java (is a gateway of course) placed in mydependency1.jar. Also I have a SpringBoot app called ApplicationMain.jar, but when i start my application this crash because ApplicationMain.jar, on runtime, not found MySFTPUploadGateway.class for autowiring as a Bean. I tried @ComponentScan("<path_to_MySFTPUploadGateway.class_on_mydependency1>"), also with @EnableIntegration but anything worked.

Reading a little about this, I Found https://github.com/spring-projects/spring-boot/issues/2037, cool, I think I have my solution with @IntegrationComponentScan ( the clue that gave me @jogo). But... This anotation only works with @EnableAutoConfiguration. This is the key.

So put your:

@EnableAutoConfiguration
@IntegrationComponentScan("<yourpathPackage>")

..and keep it 运行 !