Camel ProducerTemplate 未注入注释配置

Camel ProducerTemplate not injected with annotation config

首先,我知道有很多类似的问题 (Camel producerTemplate is not injected in spring MVC and Initializing camel from Spring annotation config),但它们对我的情况没有帮助。

我有一个使用 ProducerTemplate 发送消息的 bean:

public class SimpleProducer {

   @Produce(uri = "activemq:queue:simple")
   private ProducerTemplate activeMqProducer;

   public void send(String message) {
      activeMqProducer.sendBody(message);
   }
}

当我使用如下所示的注解驱动配置时,它会从发送方法中发送 NPE(未注入 activeMqProducer):

@Configuration
public class AnnotationConfigApp {
   public static void main(String[] args) {
      ApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfigApp.class);
      SimpleProducer simpleProducer = context.getBean(SimpleProducer.class);
      simpleProducer.send("Hello World!");
   }

   @Autowired
   private ApplicationContext ctx;

   @Bean
   public SimpleProducer simpleProducer() {
      return new SimpleProducer();
   }

   @Bean
   public CamelContext camelContext() throws Exception {
      CamelContext camelContext = new SpringCamelContext(ctx);
      camelContext.start();
      return camelContext;
   }
}

在使用等效(至少我这么认为)XML 配置时,它成功地向 ActiveMQ 发送了一条消息:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:spring="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
       <bean id="simpleProducer" class="makasprzak.so.camel.producer.testing.SimpleProducer"/>
       <spring:camelContext xmlns="http://camel.apache.org/schema/spring" id="simple.sender" />
</beans>

像这样初始化:

public class XmlConfigApp {
   public static void main(String[] args) {
      ApplicationContext context = new GenericXmlApplicationContext("context.xml");
      SimpleProducer simpleProducer = context.getBean(SimpleProducer.class);
      simpleProducer.send("Hello World!");
   }
}

我一直在玩 CamelContext 实现,尝试了 DefaultCamelContext 或一些 SpringCamelContextFactory - 没有运气。

有问题的代码在 GitHub

中可用
<properties>
    <camel.version>2.15.2</camel.version>
    <activemq.version>5.10.0</activemq.version>
    <java.version>1.8</java.version>
</properties>

我在注释配置中遗漏了什么?

您应该在 AnnotationConfigApp class

中扩展 org.apache.camel.spring.javaconfig.CamelConfiguration