简体   繁体   中英

Apache camel and spring-boot constructor injection - NPE

I have following simple spring-boot application which uses apache camel:

DemoApplication.java :

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@Component
class MySampleRoute extends SpringRouteBuilder {

    @Override
    public void configure() throws Exception {
        from("file://data/input")
                .bean(SampleProcessor1.class)
                .to("file://data/output");
    }
}

And my beans:

SampleProcessor1.java :

public class SampleProcessor1 {

    private SampleProcessor2 sampleProcessor2;

    @Autowired
    public SampleProcessor1(SampleProcessor2 sampleProcessor2) {
        this.sampleProcessor2 = sampleProcessor2;
    }

    @Handler
    public void handle(
            @Body GenericFile<String> file
    ) {

    }
}

SampleProcessor2.java :

@Component
public class SampleProcessor2 { }

When I run this application and put some file in data/input directory - I get the following NPE:

java.lang.NullPointerException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:472) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:291) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:264) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:178) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:541) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198) [camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:120) [camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) [camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:198) [camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:451) [camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:218) [camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:182) [camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:174) [camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:101) [camel-core-2.19.3.jar:2.19.3]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_111]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_111]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_111]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_111]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]

But if I try to remove constructor injection from SampleProcessor1 and use, for example field injection - then no NPE happened and everything is works fine.

SampleProcessor1.java - working version:

public class SampleProcessor1 {

    @Autowired
    private SampleProcessor2 sampleProcessor2;

    @Handler
    public void handle(
            @Body GenericFile<String> file
    ) {

    }
}

As I understand - the problem is in constructor injection - but I can't understand why.

So my question is why am I getting NPE when using constructor injection?

I use spring-boot:1.5.7.RELEASE and org.apache.camel:camel-spring-boot-starter:2.19.3


Update 1: Added source codes to github: Repository on github


Update 2:

Added breakpoint just before NPE happening method:

With constructor injection: 使用构造函数注入

Without constructor injection (using field injection): 没有构造函数注入(使用字段注入)

Spring constructor injection is not support in Camel 2.19.x or older. You need to upgrade to Camel 2.20.0 or newer.

See the release notes: http://camel.apache.org/camel-2200-release.html

  • Using Camel with Spring now supports calling Bean by their FQN name and let Spring instantiate the bean using auto-wired constructor's as opposed to only supporting a no-arg constructor.

An alternative is to refer to the bean with a bean id and let Spring more manage it via

 .bean("mySampleProcessor")

And then in the class you declare it with this name

@Component("mySampleProcessor")
public class SampleProcessor1 {
  ...
}

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