Let me start by saying that i havent done any New in my classes except for the camel route builder which is not relevant for this particular question.
I have a maven application, and I want to expose my api by camel route.
In addition I have a properties file which inject the properties by spring.
but when i use main.addRouteBuilder(new Route()) i don't know why the @Autowired annotation is return null.
This is my application context (camel-context)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"/>
<context:component-scan base-package="com.my.project.service"/>
<context:property-placeholder location="classpath:myService.prop"/>
This is my main
public class MainApp {
public static void main(String[] args) throws Exception {
MainApp mainApp = new MainApp();
mainApp.startService();
}
private static void startService() throws Exception {
try {
AppLogger.debug("Starting Camel with Spring");
Main main = new Main();
main.setApplicationContextUri("classpath:camel-context.xml");
main.addRouteBuilder(new DeleteReportRoute());
main.run();
AppLogger.debug("Camel with Spring was succesefully started");
} catch (Exception ex) {
AppLogger.error("Failed to initialize Camel with Spring);
}
}
this is my config class , and everything is injected as expected.
@Configuration()
@PropertySource("classpath:myService.prop")
public class MyConfig {
private Integer port;
private String host;
private String contextName;
public int getPort() { return port; }
public String getHost() {
return host;
}
public String getContextName() {
return contextName;
}
public MyConfig() { }
@Value("${myService.port:8888}")
public void setPort(int port) {
this.port = port;
}
@Value("${myService.host:localhost}")
public void setHost(String host) {
this.host = host;
}
@Value("${myService.contextPathName:myservice}")
public void setContextName(String contextName) {
this.contextName = contextName;
}
}
this is my route class
@Component()
public class DeleteReportRoute extends RouteBuilder {
@Autowired
public MyConfig conf; // <----------------- this is return null and i don't know why :(
/**
* Let's configure the Camel routing rules using Java code...
*/
@Override
public void configure() {
int port = conf.getPort();
String contextName = conf.getContextName();
restConfiguration().component("jetty").host("0.0.0.0").port(port).contextPath(contextName).bindingMode(RestBindingMode.auto);
rest("{tvchannel}/{tvprogram}").post("/report").to("direct:deleteReport");
from("direct:deleteReport")
.bean("messageHandler", "process")
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
.setHeader(Exchange.CONTENT_TYPE, constant("text/plain"));
}
}
note that the all of the packages are under the context:component-scan and i always get a nullPointerException for my config obj.