Reputation: 105
I'm trying to use @BeanInject
in a processor but it is always null
.
I can access the bean in a RouteBuilder
, and also in a Processor if it is defined in the RouteBuilder
but not if the Processor class is in its own file.
Is this not supported or am I missing something?
[Updated] I'm using Apache Camel 2.17.2 and the code is taken from camel-example-cdi The code below prints the object instance in the first processor but not the second. The code is run in a unit test.
public class MyRoutes extends RouteBuilder {
final static Logger LOG = LoggerFactory.getLogger(MyRoutes.class);
@Inject
@Uri("timer:foo?period=5000" )
private Endpoint inputEndpoint;
@Inject
@Uri("log:output")
private Endpoint resultEndpoint;
@BeanInject
private SomeBean someBean;
@Override
public void configure() {
from("timer:foo?period=500")
.to("bean:counterBean")
.process(new Processor(){
@Override
public void process(Exchange exchange) throws Exception {
LOG.info("[" + someBean + "]");
}
})
.process(new MyProcessor())
.to("mock:result");
}
}
The processor
public class MyProcessor implements Processor {
final static Logger LOG = LoggerFactory.getLogger(MyProcessor.class);
@BeanInject
private SomeBean someBean;
@Override
public void process(Exchange exchange) throws Exception {
LOG.info("In processor [" + someBean + "]");
}
}
Upvotes: 1
Views: 2812
Reputation: 11
In the producer you can use the lookup method from the registry
SomeBean someBean = (SomeBean)exchange.getContext().getRegistry().lookupByName("someBean");
Upvotes: 1
Reputation: 55540
If you are using CDI then you should favour using @Inject
over Camel's @BeanInject
- the latter is a poor mans substitute if you dont use CDI or Spring IoC etc, and only useable for Camel beans.
In terms of your issue then its because you create the MyProcessor
instance yourself via the new constructor. Then its standard Java that creates the instance and its not CDI or Apache Camel doing so, and therefore you cannot have dependency injection.
You can use CDI and its named beans and then have dependency injection in your processors as well - eg use standard CDI annotations. And inject your processor via @Inject
into the RouteBuilder
and call this instance from your Camel route.
Upvotes: 3