Camel producerTemplate is not injected in spring MVC

青春壹個敷衍的年華 提交于 2019-12-10 21:17:30

问题


I'm using Spring MVC and Camel in my project, but encountering an issue that the producerTemplate is not able to be Autowired. Please check details below,

File web.xml:

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

File ispatcher-servlet.xml

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

File camel-config.xml, define 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>

And here is my JAVA class:

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

Can someone point out what I'm doing wrong please?


回答1:


You probably need to make sure CammelConnectorImp is a known bean to Spring.

@Bean
public class CamelConnectorImp{ ..

(Update:)

You should probably scan for this pojo as well, so that the @Bean gets picked up:

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

or something similar will probably help.




回答2:


OK I finally figured it out. The cause was that, I didn't use the bean in Application context, instead, I created that connector by this way,

IConnector connector = new CamelConnectorImp();

This is wrong, no wonder that camelContext is not in this instance.

My fault.



来源:https://stackoverflow.com/questions/13265703/camel-producertemplate-is-not-injected-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!