Spring-integration网关无法自动装配,也没有找到合格的bean

时间:2015-02-13 22:21:07

标签: spring-integration spring-bean

我正在尝试将spring批处理的输出发送到RabbitMQ。为了避免对Rabbitmq的硬依赖,我使用了Is there an API in spring-xd to write to a message bus?中建议的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" />

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

我试过四处寻找,但Gateway的大多数示例(在spring-integration示例中)通过ApplicationContext调用它。我找到http://java.dzone.com/articles/spring-integration-gateways-1,并添加了我认为我遗失的任何注释,但无法使其正常工作。

非常感谢任何指针。

3 个答案:

答案 0 :(得分:1)

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

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

意味着它在自己的背景下。你是否<import/>进入你的工作环境?

我通常打开org.springframework的调试日志来调试这样的问题;它为豆子布线发出了大量的日志。

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

答案 1 :(得分:1)

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

  

@IntegrationComponentScan(&#34; path.to.your.gateway&#34)

答案 2 :(得分:0)

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

我将使用一个名为@MessagingGateway的带注释的类MySFTPUploadGateway.java接口(当然是网关)放置在mydependency1.jar中。另外,我还有一个名为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>")

..并保持其运行!