无法自动装配 Spring 集成网关,找不到符合条件的 bean

Ali*_*ice 4 spring-integration spring-bean

我正在尝试将 spring 批处理的输出发送到 RabbitMQ。为了避免对 Rabbitmq 的硬依赖,我按照在 spring-xd是否有 API 来写入消息总线中的建议使用了 spring-integration . 我在将消息推送到 RabbitMQ 时一切正常,直到我决定通过 Spring 自动装配或实例化 bean。

<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" />
Run Code Online (Sandbox Code Playgroud)

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();
Run Code Online (Sandbox Code Playgroud)

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

@Autowired
TestService service;
Run Code Online (Sandbox Code Playgroud)

我收到以下异常:

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
Run Code Online (Sandbox Code Playgroud)

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

任何指针都非常感谢。

小智 7

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

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


小智 5

我知道这是一个老问题,但我有一个类似的问题:

我将使用一个名为(当然是网关)的带注释的类@MessagingGateway接口,放置在 mydependency1.jar 中。MySFTPUploadGateway.java我还有一个名为 ApplicationMain.jar 的 SpringBoot 应用程序,但是当我启动应用程序时,此崩溃是因为ApplicationMain.jar在运行时未找到MySFTPUploadGateway.class自动装配为 Bean 的功能。我@ComponentScan("<path_to_MySFTPUploadGateway.class_on_mydependency1>")也尝试过,@EnableIntegration但一切都有效。

阅读了一些相关内容,我发现https://github.com/spring-projects/spring-boot/issues/2037,酷,我想我有我的解决方案 @IntegrationComponentScan(给我@jogo的线索)。但是...此注释仅适用于@EnableAutoConfiguration。这是关键。

所以把你的:

@EnableAutoConfiguration
@IntegrationComponentScan("<yourpathPackage>")
Run Code Online (Sandbox Code Playgroud)

..并保持运行!