首页 > 解决方案 > 在骆驼流程语句中忽略自动装配注释

问题描述

我需要在流程步骤中实现数据库连接和查询。所以,我在 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]

标签: apache-camelspring-camel

解决方案


当然它是空的,因为你是通过新的构造函数自己创建实例:

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 实例。


推荐阅读