Megha Megha - 4 months ago 18
Java Question

@Service and @Repository beans not initializing in Spring boot and Apache camel

In my camel and spring boot application, I have a simple route: folder->processor->folder.

In the processor the service and repository are always null. In the test classes these are not null.

@Override
public void configure() throws Exception
{
from("file:input")
.log("from file")
.process(new MyProcessor())
.to("file:destination")
.log("to destination")`
.end();
}


Am I missing anything. Why are the respository and service beans null in the processor but work fine in the test classes.

Answer

You are manually creating a processor via new MyProcessor(), meaning Spring will not autowire the dependencies for you.

You should use Camel Bean support instead:

@Override
public void configure() throws Exception
{
    from("file:input")
    .log("from file")
    .bean("myProcessor")
    .to("file:destination")
    .log("to destination")`
    .end();
}

Or, if your MyProcessor bean implements Camel's Processor, you could do something like this:

@Autowired
private MyProcessor processor;

@Override
public void configure() throws Exception
{
    from("file:input")
    .log("from file")
    .processor(processor)
    .to("file:destination")
    .log("to destination")`
    .end();
}