gpt4 book ai didi

java - Apache camel 和 spring-boot 构造函数注入(inject) - NPE

转载 作者:行者123 更新时间:2023-11-29 06:51:32 24 4
gpt4 key购买 nike

我有以下使用 apache camel 的简单 spring-boot 应用程序:

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");
}
}

还有我的 bean :

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 { }

当我运行此应用程序并将一些文件放入 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]

但是,如果我尝试从 SampleProcessor1 中移除构造函数注入(inject)并使用,例如字段注入(inject) - 那么就不会发生 NPE,并且一切正常。

SampleProcessor1.java - 工作版本:

public class SampleProcessor1 {

@Autowired
private SampleProcessor2 sampleProcessor2;

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

}
}

据我了解 - 问题在于构造函数注入(inject) - 但我不明白为什么。

所以我的问题是为什么在使用构造函数注入(inject)时会出现 NPE?

我使用 spring-boot:1.5.7.RELEASEorg.apache.camel:camel-spring-boot-starter:2.19.3


更新 1:将源代码添加到 github:Repository on github


更新 2:

在 NPE 发生方法之前添加断点:

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

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

最佳答案

Camel 2.19.x 或更早版本不支持 Spring 构造函数注入(inject)。您需要升级到 Camel 2.20.0 或更新版本。

请参阅发行说明:http://camel.apache.org/camel-2200-release.html

  • 将 Camel 与 Spring 结合使用现在支持通过 FQN 名称调用 Bean,并让 Spring 使用自动连接的构造函数实例化 bean,而不是仅支持无参数构造函数。

另一种方法是使用 bean id 来引用 bean,让 Spring 更多地管理它

 .bean("mySampleProcessor")

然后在类中用这个名字声明它

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

关于java - Apache camel 和 spring-boot 构造函数注入(inject) - NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46747882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com