如何从bean.xml获取bean值

时间:2013-09-21 09:32:27

标签: java spring activemq apache-camel

我们正在努力编写3个消费者,使用Activemq将来自ThermalMap队列名称的消息作为Apache Camel and Spring消费。

我是Spring的新手,我试图从bean.xml获取特定的beanId(“amqLinkId”)值。为此我编写了以下3个文件。

VariablesDeclarations.java

public class VariablesDeclarations {
private String amqLink;
public String getAmqLink() {
return amqLink;
}
public void setAmqLink(String amqLink) {
this.amqLink = amqLink;
}
}

FirstConsumer.java

import org.apache.camel.CamelContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FirstConsumer {
        public static void main(String[] args) {
     ApplicationContext contextObject=new ClassPathXmlApplicationContext("bean.xml");
        //Forwarding cursor to ConsumersMiddileWare class using CamelContext
        CamelContext camelObject=contextObject.getBean("activeContext", CamelContext.class);
        System.out.println("H");
}
} 

ConsumersMiddileware.java

import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConsumersMiddileWare extends RouteBuilder{
@Override
public void configure() throws Exception {
    System.out.println("Bye");
    BeanFactory beanFactory=new ClassPathXmlApplicationContext(new String[]{"bean.xml"});
    VariablesDeclarations vd=(VariablesDeclarations) beanFactory.getBean("amqLinkId");
    System.out.println(vd.getAmqLink());
}
 }

bean.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
                    http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
                    http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd
                    ">

<!-- Creating camelContext element for forwarding to another ConsumersMiddileware class -->
<camelContext id="activeContext" xmlns="http://camel.apache.org/schema/spring">
    <routeBuilder ref="consumerMiddileWareContext" />
</camelContext>
<bean id="consumerMiddileWareContext" class="ActivemqPackage.ConsumersMiddileWare"></bean>

<!-- Following bean created for getting variable values into classes-->
<bean id="amqLinkId" class="ActivemqPackage.VariablesDeclarations">
    <property name="amqLink" value="activemq:queue:ThermalMap"></property>
</bean>
</beans>

WorkFlow:

如果您运行FirstConsumer.java,它应该转发到ConsumersMiddileWare.java。我希望从amqLinkId中找到bean.xml值并分配给任何String对象。

如果我找到amqLinkId值,则稍后我想在ConsumersMiddileWare.java中编写以下代码

  String activeMqURI=vd.getAmqLink();
  from( activeMqURI).to("bean:activemqProcessor?method=processMessage");

问题:

如果我触发FirsrConsumer.java,则会转发到ConsumersMiddileWare.java。它会继续打印Bye而不是打印amqLinkId值,如下所示。

 Bye
 Bye
 Bye
 Bye
 .
 .
任何人都可以解释清楚。

由于

2 个答案:

答案 0 :(得分:1)

问题是您的项目无法找到slf4j-simple-1.6.2.jar。检查您是否在我们的项目构建路径中拥有它。如果没有添加它然后检查。此外,您报告的实际问题和您之后编辑的问题也完全不同。

答案 1 :(得分:1)

您可以使用SpringRouteBuilder而不是RouteBuilder。这个类有一些额外的方法可以从spring应用程序上下文中获取bean(例如spring xml文件)

public class ConsumersMiddileWare extends SpringRouteBuilder{
@Override
public void configure() throws Exception {
    VariablesDeclarations vd = lookup("amqLinkId", VariablesDeclarations.class)' 
 }

另一种方法是在常规的RouteBuilder中使用camelContext.getRegistry

public class ConsumersMiddileWare extends RouteBuilder{
@Override
public void configure() throws Exception {
    VariablesDeclarations vd = context().getRegistry().lookup("amqLinkId", VariablesDeclarations.class)' 
 }