apache-camel - 在骆驼流程语句中忽略自动装配注释
问题描述
我需要在流程步骤中实现数据库连接和查询。所以,我在 bean 属性中定义了数据源。我尝试使用 jdbctemplate。但结果返回 java.lang.NullPointException。
骆驼会忽略流程语句中的自动装配注释吗?如果有其他解决方案,请告诉我。
谢谢你。
CamelContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
...
http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean class="com.ktds.openmzn.common.bean.ProcFormat" id="procFormat"/>
<bean class="com.ktds.openmzn.common.bean.ProcessDistributor" id="splitChannel"/>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="${spring.datasource.driver-class-name}"/>
<property name="url" value="${spring.datasource.url}"/>
<property name="username" value="${spring.datasource.username}"/>
<property name="password" value="${spring.datasource.password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="com.ktds.openmzn.common.bean.FilePathProcessor" id="filePathProcessor"/>
...
<camelContext id="camelContext-f611cb6c-d516-4346-9adc-5512d327a88d"
trace="true" xmlns="http://camel.apache.org/schema/spring">
<camel:route id="fixed_processor">
<camel:from id="_from1" uri="timer:fromPollTimer?period=20000"/>
<camel:process id="_sourceDirectory" ref="filePathProcessor"/>
...
文件路径处理器.java
public class FilePathProcessor implements Processor {
...
@Override
public void process(Exchange exchange) throws Exception {
List<Map<String, Object>> rows = SetFilePath.getInstance().getPathList("aaaa");
设置文件路径.java
@ManagedResource
public class SetFilePath {
private static SetFilePath instance = null;
private String sourceDirectory;
private String targetDirectory;
@Autowired
private JdbcTemplate jdbcTemplate;
public static SetFilePath getInstance() {
if(instance == null) {
instance = new SetFilePath();
}
return instance;
}
结果
Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId ProcessorId Processor
Elapsed (ms)
[fixed_processor ] [fixed_processor ] [timer://fromPollTimer?period=20000 ] [ 0]
[fixed_processor ] [_sourceDirectory ] [ref:filePathProcessor ] [ 0]
Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
java.lang.NullPointerException: null
at com.ktds.openmzn.common.bean.FilePathProcessor.process(FilePathProcessor.java:20) ~[classes/:na]
at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63) ~[camel-core-2.23.1.jar:2.23.1]
解决方案
当然它是空的,因为你是通过新的构造函数自己创建实例:
instance = new SetFilePath();
它@Autowired
来自 spring 框架,你基本上需要使用 spring 来为你创建这个 bean。
有不同的方法可以做到这一点,例如<bean>
在 XML 文件中创建一个,然后通过一个 setter 在你的处理器上配置它<property>
。
您也可以让 spring/camel 创建 bean 实例而不是 new 构造函数,但这需要一些 Camel API 来执行此操作
public static SetFilePath getInstance(CamelContext camel)
if (instance == null) {
instance = camel.getInjector().newInstance(SetFilePath.class);
}
然后,它将通过注入器通过自动装配的 spring 框架创建 bean 实例。
推荐阅读
- ffmpeg - 使用 adpcm_ima_wav 编解码器将原始音频转换为 mp3(wav)
- c++ - 有人可以帮忙找出这段代码中 s 运行时错误的原因吗?
- google-apps-script - 如何使用谷歌脚本创建谷歌表单模板?
- oracle - Oracle:约束防止插入超过 N(变量)行
- machine-learning - 从在本地运行 ML 训练和测试迁移到 Google Cloud
- angular - 有没有办法用 ion-select interface="action-sheet" 禁用第一个选项?
- python - 将单行替换为多行python
- python - 将鼠标光标位置设置为对象标题
- c# - 以 7 个字节保存的日期。需要破译这个日期
- python - 在 groupby 之后获取组名作为列表