简体   繁体   中英

ProducerTemplate is always null with camel and spring-boot

I have a requirement to integrate Camel with spring-boot , to achieve same I am using below code to build ProducerTemplate for bean integration but ProducerTemplate will always NULL.

Java Code

@RestController
public class TestController implements ProductSummaryApi {

    @EndpointInject(uri = "direct:test")
    ProducerTemplate testRoute;

    @RequestMapping(value = "/v1/test", method = RequestMethod.GET)
    public String test(){
        System.out.println("Route: " + testRoute);
        return "Test";
    }
}

Line System.out.println("Route: " + testRoute); prints NULL

pom.xml

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring-boot-starter</artifactId>
    <version>2.21.0</version>
</dependency>

Any hint appreciated, why the application is not able to build ProducerTemplate object.

  • If you rely on predefined ProducerTemplate from spring boot camel, @Produce relies on Spring for dependency injection of Producer template. Which means that the service that autowires the producer template must register itself as the bean.
    Check if your service class is annotated with @Component or it's sub-types.
  • If you programmatically create ProducerTemplate and have associated with a context(assuming you have registered as bean using @Bean ), use @Produce(context="contextName1") .

Check your component scan path. That's mostly your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM