После долгих поисков я нашел решение на основе Spring BeanFactory
. Основным препятствием являются круговые зависимости (Компонент -> Конечная точка -> Компонент | Конечная точка -> Производитель -> Конечная точка). Сначала я представил класс Spring Configuration
:
@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 компонентом Spring с аннотацией @Component
, поэтому я могу добавить BeanFactory
для создания экземпляра. класса MyCustomEndpoint
по требованию (prototype
область).
@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 {
}
}
Обратите внимание, что компонент Camel MyCustomComponent
уже зарегистрирован в resources/META-INF/services/org/apache/camel/component/myscheme
:
class=<fqn-of-MyCustomComponent>