如何使用SpringBootJava配置编写定制的ApacheCamel组件/端点
我正在寻找一个示例/文档,介绍如何使用Java配置中的Spring Boot实现定制的Apache Camel如何使用SpringBootJava配置编写定制的ApacheCamel组件/端点,spring,apache-camel,spring-camel,Spring,Apache Camel,Spring Camel,我正在寻找一个示例/文档,介绍如何使用Java配置中的Spring Boot实现定制的Apache Camel组件和端点。我不知道如何注释这些类,你能举个例子吗。 如果不使用Spring,我的(工作)代码如下所示: public class MyCustomComponent extends DefaultComponent { @Override protected Endpoint createEndpoint(String uri, String remaining, Ma
组件
和端点
。我不知道如何注释这些类,你能举个例子吗。
如果不使用Spring,我的(工作)代码如下所示:
public class MyCustomComponent extends DefaultComponent {
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
Endpoint endpoint = new MyCustomEndpoint(uri, this);
setProperties(endpoint, parameters);
return endpoint;
}
}
经过大量搜索,我在Spring的
BeanFactory
的基础上找到了一个解决方案。主要障碍是循环依赖关系(组件->端点->组件|端点->生产者->端点)。首先,我介绍了一个Spring配置
类:
@Configuration
public class ComponentConfiguration {
@Bean("myCustomEndpoint")
@Scope("prototype")
public MyCustomEndpoint myCustomEndpoint(String uri, MyCustomComponent component) {
MyCustomEndpoint endpoint = new MyCustomEndpoint(uri, component);
return endpoint;
}
@Bean("myCustomProducer")
@Scope("prototype")
public MyCustomProducer myCustomProducer(MyCustomEndpoint endpoint) {
return new MyCustomProducer(endpoint);
}
}
使定制的Camel组件成为带有@component
注释的Spring组件,这样我就能够注入BeanFactory
来根据需要创建MyCustomEndpoint
类的实例(prototype
scope)
请注意,Camel组件已在resources/META-INF/services/org/apache/Camel/component/myscheme
中注册:
class=<fqn-of-MyCustomComponent>
类=
只需查看现有的Camel组件即可获得一个示例,而且isSingleton应该返回true。@克劳斯·易卜生感谢您的回答。如何将Springbean注入MyCustomProducer
@Configuration
public class ComponentConfiguration {
@Bean("myCustomEndpoint")
@Scope("prototype")
public MyCustomEndpoint myCustomEndpoint(String uri, MyCustomComponent component) {
MyCustomEndpoint endpoint = new MyCustomEndpoint(uri, component);
return endpoint;
}
@Bean("myCustomProducer")
@Scope("prototype")
public MyCustomProducer myCustomProducer(MyCustomEndpoint endpoint) {
return new MyCustomProducer(endpoint);
}
}
@org.springframework.stereotype.Component
public class MyCustomComponent extends DefaultComponent {
@Autowired
private BeanFactory beanFactory;
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
MyCustomEndpoint endpoint = (MyCustomEndpoint) beanFactory.getBean("myCustomEndpoint", uri, this);
setProperties(endpoint, parameters);
return endpoint;
}
}
public class MyCustomEndpoint extends DefaultEndpoint {
@Autowired
private BeanFactory beanFactory;
public MyCustomEndpoint(String uri, MyCustomComponent component) {
super(uri, component);
}
@Override
public Producer createProducer() throws Exception {
MyCustomProducer producer = (MyCustomProducer) beanFactory.getBean("myCustomProducer", this);
return producer;
}
@Override
public Consumer createConsumer(Processor processor) throws Exception {
throw new UnsupportedOperationException("Not implemented yet: MyCustomEndpoint#createConsumer");
}
@Override
public boolean isSingleton() {
return true;
}
}
public class MyCustomProducer extends DefaultProducer {
// Now, I am able to inject some other Spring beans
@Autowired
private AnotherSpringBean bean;
public MyCustomProducer(Endpoint endpoint) {
super(endpoint);
}
@Override
public void process(Exchange exchange) throws Exception {
}
}
class=<fqn-of-MyCustomComponent>