java - 通过java代码向Spring 3添加组件

标签 java spring activemq apache-camel

有没有办法通过Spring的applicationConfig XML中的javacode添加ActiveMQ组件?

我的主要目标是从外部属性文件中获取“brokerURL”。但属性文件不是标准的属性文件,它基于 XML,因此必须对其进行适当的解析并获取属性。

<!-- COMPONENT BEANS -->
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="connectionFactory">
        <bean class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="tcp://localhost:61616"/>
        </bean>
    </property>
</bean>

最佳答案

有几种方法。

一个是真正的程序化,如下所示:

org.apache.activemq.camel.component.ActiveMQComponent amq = new org.apache.activemq.camel.component.ActiveMQComponent();
amq.setConnectionFactory(new ActiveMQConnectionFactory(parseOddXml(brokerXMLConfigFile)));
camelContext.addComponent("activemq", amq);

假设您有一个 Camel 上下文感知 bean 来初始化您的组件。

否则,您也许可以从其他地方连接连接工厂并将其注入(inject) ActiveMQ 组件上的 XML 配置中。

也许是这样的

 @Configuration
 class MyAMQConfig{
   public @Bean ActiveMQConnectionFactory createCF(){
      String brokerURI = parseOddXml(brokerConfigFile); // or whatever logic here.
      return new ActiveMQConnectionFactory(brokerURI);
   }
 }

然后 XML 中类似这样的内容:

<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="connectionFactory" ref="activeMQConnectionFactory"/>
</bean>

或者任何其他方式,因为有多种方法可以连接 bean 并与 Camel 上下文交互。

关于java - 通过java代码向Spring 3添加组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11215136/

相关文章:

java - Spring MVC 4 : Interceptor not being able to set Response header

activemq - 了解 Apache ActiveMQ

java - ActiveMQ 消息不断被清除

java - 当 JMS 消息被使用时收到通知

java - 如何在 Activity 开始时在进度条上显示进度

java - 如何减少 Apache CXF 客户端 stub 对象的内存大小?

java - Spring MVC 在端点上使用相同的路径来返回不同的内容?

java - Play JPA 查询示例

java - 在 Mac 上使用 selenium Chromedriver 3.11.0 自动下载文件

java - Spring @Autowired 链和常规 'new XYZ()' 实例化