将蓝图bean注入camel处理器

时间:2016-07-07 17:45:20

标签: dependency-injection osgi javabeans blueprint

请帮帮我。如何将blueprint.xml中声明的bean注入Camel处理器?         我正在创建OSGI包以便在Jboss Fuse 6.1容器中进行部署。我目前的blueprint.xml:

 <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" />
<bean id="Sender" class="com.test.Sender" scope="prototype">
         <property name="senderId" value="admin" />
         <property name="password" value="admin" />
    </bean>
          <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>

这是骆驼处理器:

import org.apache.aries.blueprint.annotation.Inject; 
    public class MyProcessor implements Processor {

    @Inject
    private Sender sender;

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

但是我得到了NullPointerException。那么可以将容器创建的bean Sender注入MyProccessor吗?如何做到这一点?

1 个答案:

答案 0 :(得分:0)

你想通过注射做什么?

通常处理器使用如下:

 public void process(Exchange x) throws Exception {
    from("direct://start")
    .to("MyProcessor")        
    log.info("test: " +sender.getSenderId());
 }

除了根据http://camel.apache.org/bean-integration.html注入处理器不支持。