【问题标题】:autowiring spring bean in camel processor在骆驼处理器中自动装配弹簧豆
【发布时间】:2016-07-07 11:59:51
【问题描述】:

我正在构建 OSGI 包以在 Jboss Fuse 6.1 容器中运行。项目包含 blueprint.xml(在 src/main/resourceces/OSGI-INF/blueprint 中)。内容是:

 <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:camel="http://camel.apache.org/schema/blueprint"
               xmlns:camelcxf="http://camel.apache.org/schema/blueprint/cxf"
               xmlns:cxf="http://cxf.apache.org/blueprint/core"
               xmlns:jaxws="http://cxf.apache.org/blueprint/jaxws"

               xsi:schemaLocation="
           http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
             http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd
           http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
            http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd
     http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd"
    >
<bean id="MyProcessor" class="com.test.MyProcessor" />
          <camelContext trace="false" id="blueprintContext" xmlns="http://camel.apache.org/schema/blueprint">
            <route>
                <from uri="activemq:queue:paymentsqueue?username=admin&amp;password=admin" id="NotificationRoute">
                    <description/>
                </from>
                <log message="The message contains ${body}"/>
                <process ref="MyProcessor"/>
            </route>
        </camelContext>
    </blueprint>

我的目标是在 MyProcessor 中使用 spring bean。这是 MyProcessor 的代码:

public class MyProcessor implements Processor {

@Aurowired
private Sender sender;

@Override
public void process(Exchange x) throws Exception {
    log.info("test: " +sender.getSenderId());
}

}

但它给了我空指针异常。我做错了什么?

这是我的spring配置文件的内容(我放在src/main/resourceces/META-INF/spring)

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="com.username"/>
<bean id="sender" class="com.username.Sender" scope="prototype">
     <property name="senderId" value="sendername" />
     <property name="password" value="senderpassword" />
</bean>

【问题讨论】:

  • 蓝图bean中一般可以同时使用spring bean吗?在我的示例中,camel route 和处理器由蓝图创建,但自动装配 bean 由 spring 创建。

标签: spring apache-camel osgi jbossfuse blueprint-osgi


【解决方案1】:

我认为你还没有像 spring 一样将处理器 MyProcessor 声明为 bean

<bean id="myProcessor"
        class="com.test.MyProcessor" />

那么你就可以把它当做

<process ref="myProcessor"/>

还有

@Aurowired
private Sender sender;

应该是@Autowired(这应该是一个错字但指出来。)

【讨论】:

  • 是的,我在复制上面的代码时出错了。这个 bean 在蓝图中声明。我已经编辑了。
猜你喜欢
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多