3

我在我的项目中使用 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);
...
}

有人可以指出我做错了什么吗?

4

2 回答 2

1

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

@Bean
public class CamelConnectorImp{ ..

(更新:)

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

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

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

于 2012-11-07T10:06:44.487 回答
0

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

IConnector connector = new CamelConnectorImp();

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

我的错。

于 2012-11-07T10:29:12.660 回答