简体   繁体   English

Apache骆驼和Spring-Boot构造函数注入-NPE

[英]Apache camel and spring-boot constructor injection - NPE

I have following simple spring-boot application which uses apache camel: 我有以下简单的使用Apache骆驼的spring-boot应用程序:

DemoApplication.java : 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 : 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 : SampleProcessor2.java

@Component
public class SampleProcessor2 { }

When I run this application and put some file in data/input directory - I get the following NPE: 当我运行此应用程序并将一些文件放在data/input目录中时,我得到以下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删除构造函数注入并使用(例如,字段注入),则不会发生NPE,并且一切正常。

SampleProcessor1.java - working version: SampleProcessor1.java工作版本:

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? 所以我的问题是为什么在使用构造函数注入时会得到NPE?

I use spring-boot:1.5.7.RELEASE and org.apache.camel:camel-spring-boot-starter:2.19.3 我使用spring-boot:1.5.7.RELEASEorg.apache.camel:camel-spring-boot-starter:2.19.3


Update 1: Added source codes to github: Repository on github 更新1:向github添加了源代码:github上的存储库


Update 2: 更新2:

Added breakpoint just before NPE happening method: 在NPE发生方法之前添加断点:

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

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

Spring constructor injection is not support in Camel 2.19.x or older. 在Camel 2.19.x或更早的版本中不支持Spring构造函数注入。 You need to upgrade to Camel 2.20.0 or newer. 您需要升级到Camel 2.20.0或更高版本。

See the release notes: http://camel.apache.org/camel-2200-release.html 请参阅发行说明: 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. 将Camel与Spring一起使用现在支持通过FQN名称调用Bean,并且让Spring使用自动连接的构造函数实例化Bean,而不是仅支持无参数的构造函数。

An alternative is to refer to the bean with a bean id and let Spring more manage it via 一种替代方法是使用bean id引用bean,并让Spring通过以下方式对其进行管理

 .bean("mySampleProcessor")

And then in the class you declare it with this name 然后在类中使用此名称声明它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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