Apache Camel:在 Spring 操作之前设置对象依赖关系

Apache Camel: set an object dependency before operation with Spring

在这个例子中,我试图在调用 businessLogic 之前设置对象依赖关系。我收到一个空指针,因为未设置 'consumer' 对象。

这是示例的基础,主要是尝试使用 Spring DSL。

http://camel.apache.org/polling-consumer

部分:基于定时器的轮询消费者

这是我的 camel/spring 配置:

  <bean id="simpleOutboxMessageConsumer" class="org.berlin.camel.esb.logs.mq.SimplePrintMessageConsumer"/>


  <!-- Continue with spring dsl for ESB -->
  <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring">

    <!-- Define a MQ consumer template -->
    <consumerTemplate id="consumer" />

....
   </camelContext>

      <route id="fromOutboxAndConsume">           
          <from uri="timer://foo?period=30000" />
          <to uri="bean:simpleOutboxMessageConsumer?method=businessLogic" />                                        
      </route>

Java代码

@Component
public class SimplePrintMessageConsumer {   
    private static final Logger logger = Logger.getLogger(SimplePrintMessageConsumer.class);        
    private int count;  
    @Autowired 
    private ConsumerTemplate consumer;          
    public void setConsumer(final ConsumerTemplate consumer) {
        this.consumer = consumer;
    }
    public void businessLogic() {
        logger.info("Launching business logic to consume outbox, blocking until we get a message >>>");
        while (true) {
            // Consume the message
            final String msg = consumer.receiveBody("activemq:queue.outbox", 3000, String.class);
            logger.info("Printing message found from queue: " + msg);
            if (msg == null) {
                // no more messages in queue
                break;
            }        
        }
    }   
}

消费对象的使用处有一个空指针。我在想 spring 不仅仅是正确地自动装配那个 bean。即使我不使用 spring,我如何将消费者模板对象传递给这个 bean?

这应该有效

<bean id="simpleOutboxMessageConsumer" class="....SimplePrintMessageConsumer">
<property name="consumer" ref="consumer"/>
</bean>

删除@AutoWire,我正在检查为什么@Autowire 不能正常工作