问题
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