gpt4 book ai didi

java - Camel ProducerTemplate 未注入(inject)注释配置

转载 作者:行者123 更新时间:2023-11-30 03:25:47 25 4
gpt4 key购买 nike

首先,我知道有非常相似的问题( Camel producerTemplate is not injected in spring MVCInitializing 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(未注入(inject) 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 类中扩展 org.apache.camel.spring.javaconfig.CamelConfiguration

关于java - Camel ProducerTemplate 未注入(inject)注释配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30255698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com