我正在将应用程序迁移到Spring boot 3.0,但我在我的一个服务(注解@Service se Spring Boot)中注入ProducerTemplate时遇到问题。我使用的是Camel当前Spring Boot版本的Spring Boot启动器:2.7.5
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.15.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-rabbitmq-starter</artifactId>
<version>3.15.0</version>
</dependency>
我的ProducerTemplate被注入到扩展RouteBuilder的服务的构造函数中
@Service
public class MyServiceImpl extends RouteBuilder implements MyService {
private final ProducerTemplate producerTemplate;
@Autowired
public MyServiceImpl(ProducerTemplate producerTemplate) {
this.producerTemplate = producerTemplate;
}
启动时,应用程序崩溃并出现以下消息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.mycompany.ServiceImpl required a bean of type 'org.apache.camel.ProducerTemplate' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.apache.camel.ProducerTemplate' in your configuration.
我不太清楚我应该如何接受它。我想我必须使用依赖项
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>3.15.0</version>
</dependency>
而不是
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.15.0</version>
</dependency>
但是我真的不知道我应该如何实例化ProducerTemplate和CamelContext?
我试着不相信这样的事情:
@Configuration
public class CamelConfig {
@Bean
public CamelContext getCamelContext() throws Exception {
CamelContext context = new DefaultCamelContext();
if (context.isStopped()) context.start();
return context;
}
@Bean
public ProducerTemplate getProducerTemplate() throws Exception
{
final ProducerTemplate template = getCamelContext().createProducerTemplate();
return template;
}
}
@Service
public class MyServiceImpl implements MyService {
private final ProducerTemplate producerTemplate;
private CamelContext context;
@Autowired
public MyServiceImpl(ProducerTemplate producerTemplate, CamelContext context) {
this.producerTemplate = producerTemplate;
this.context = context;
try {
this.context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:update-legacy")
.to("bean:updateJdbc");
应用程序失败并出现以下错误消息:
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: bean://updateJdbc due to: No bean could be found in the registry for: updateJdbc
从此链接中,Apache Camel支持Spring boot 3,问题是Apache Camel不支持Spring Boot 3。它将从Apache Camel 4. x支持它。如果您使用的是Apache Camel 3.x,请使用Spring Boot 2.7.x。