【问题标题】:Camel producerTemplate is not injected in spring MVC骆驼生产者模板未在spring MVC中注入
【发布时间】:2012-10-27 06:31:33
【问题描述】:

我在我的项目中使用 Spring MVC 和 Camel,但遇到了 producerTemplate 无法自动装配的问题。请在下面查看详细信息,

文件 web.xml:

<context-param>
 <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

文件 ispatcher-servlet.xml

<import resource="camel-config.xml"/>

文件camel-config.xml,定义camelContext

<context:component-scan base-package="com.myproject.camel.routes"/>
<camelContext xmlns="http://camel.apache.org/schema/spring" id="myproject.camel">
     <contextScan/>
 <template id="producerTemplate"/>
</camelContext>

这是我的 JAVA 类:

package com.myproject.connector.camel;
public class CamelConnectorImp{
    @Autowired
    private ProducerTemplate producerTemplate; //This is null after starting
    producerTemplate.requestBodyAndHeaders(serviceEndpoint,request, headers);
...
}

谁能指出我做错了什么?

【问题讨论】:

    标签: spring model-view-controller apache-camel


    【解决方案1】:

    您可能需要确保 CammelConnectorImp 是 Spring 的已知 bean。

    @Bean
    public class CamelConnectorImp{ ..
    

    (更新:)

    您可能也应该扫描这个 pojo,以便获取 @Bean:

    <context:component-scan base-package="com.myproject.camel.routes,com.myproject.connector.camel"/> 
    

    或类似的东西可能会有所帮助。

    【讨论】:

      【解决方案2】:

      好的,我终于想通了。 原因是,我没有在 Application 上下文中使用 bean,而是通过这种方式创建了该连接器,

      IConnector connector = new CamelConnectorImp();
      

      这是错误的,难怪camelContext不在这个实例中。

      我的错。

      【讨论】:

      • 是的,就像我的回答所说,您应该选择 CamelConnectorImp 作为 bean。
      猜你喜欢
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 2018-07-28
      • 2014-08-22
      • 2018-01-27
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多