2012-11-07 69 views
3

我在我的项目中使用Spring MVC和骆驼,但遇到生产者模板不能自动装配的问题。请检查下面的细节,骆驼生产者模板不是在春天注入MVC​​

文件的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"/> 

文件骆驼-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); 
... 
} 

有人能指出我做错了吗?

回答

1

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

@Bean 
public class CamelConnectorImp{ .. 

(更新:)

你或许应该扫描该POJO以及,使@Bean被拾起:

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

或类似可能会帮助一些东西。

+0

谢谢佩特,我试过使用@Component,但它没有工作 – Uda

0

好的,我终于明白了。 的原因是,我没有使用在应用程序上下文中的bean,相反,我创造了这个方式,连接器,

IConnector connector = new CamelConnectorImp(); 

这是不对的,难怪camelContext是不是在这种情况下。

我的错。

+1

是的,就像我的回答说的,你应该拿起CamelConnectorImp作为bean。 –

+0

谢谢Petter! – Uda